eslint-plugin-react

eslint-plugin-react

ESLint插件为React项目提供全面代码规范检查

eslint-plugin-react是为React项目开发的ESLint插件。它提供丰富的规则集,用于检测和修复React代码中的潜在问题,涵盖JSX语法、组件定义和props使用等方面。插件包含多种配置选项,如推荐配置和自定义规则设置。它能够检查JSX语法、组件定义、props使用等多个方面,帮助开发者发现和修复潜在的代码问题。采用eslint-plugin-react可以有效提升React项目的代码质量和一致性。

eslintReactlinting配置规则Github开源项目

eslint-plugin-react <sup>[![Version Badge][npm-version-svg]][package-url]</sup>

===================

[![github actions][actions-image]][actions-url] [![Maintenance Status][status-image]][status-url] [![NPM version][npm-image]][npm-url] [![Tidelift][tidelift-image]][tidelift-url]

React specific linting rules for eslint

Installation

npm install eslint eslint-plugin-react --save-dev

It is also possible to install ESLint globally rather than locally (using npm install -g eslint). However, this is not recommended, and any plugins or shareable configs that you use must be installed locally in either case.

Configuration (legacy: .eslintrc*) <a id="configuration"></a>

Use our preset to get reasonable defaults:

"extends": [ "eslint:recommended", "plugin:react/recommended" ]

If you are using the new JSX transform from React 17, extend react/jsx-runtime in your eslint config (add "plugin:react/jsx-runtime" to "extends") to disable the relevant rules.

You should also specify settings that will be shared across all the plugin rules. (More about eslint shared settings)

{ "settings": { "react": { "createClass": "createReactClass", // Regex for Component Factory to use, // default to "createReactClass" "pragma": "React", // Pragma to use, default to "React" "fragment": "Fragment", // Fragment to use (may be a property of <pragma>), default to "Fragment" "version": "detect", // React version. "detect" automatically picks the version you have installed. // You can also use `16.0`, `16.3`, etc, if you want to override the detected value. // Defaults to the "defaultVersion" setting and warns if missing, and to "detect" in the future "defaultVersion": "", // Default React version to use when the version you have installed cannot be detected. // If not provided, defaults to the latest React version. "flowVersion": "0.53" // Flow version }, "propWrapperFunctions": [ // The names of any function used to wrap propTypes, e.g. `forbidExtraProps`. If this isn't set, any propTypes wrapped in a function will be skipped. "forbidExtraProps", {"property": "freeze", "object": "Object"}, {"property": "myFavoriteWrapper"}, // for rules that check exact prop wrappers {"property": "forbidExtraProps", "exact": true} ], "componentWrapperFunctions": [ // The name of any function used to wrap components, e.g. Mobx `observer` function. If this isn't set, components wrapped by these functions will be skipped. "observer", // `property` {"property": "styled"}, // `object` is optional {"property": "observer", "object": "Mobx"}, {"property": "observer", "object": "<pragma>"} // sets `object` to whatever value `settings.react.pragma` is set to ], "formComponents": [ // Components used as alternatives to <form> for forms, eg. <Form endpoint={ url } /> "CustomForm", {"name": "SimpleForm", "formAttribute": "endpoint"}, {"name": "Form", "formAttribute": ["registerEndpoint", "loginEndpoint"]}, // allows specifying multiple properties if necessary ], "linkComponents": [ // Components used as alternatives to <a> for linking, eg. <Link to={ url } /> "Hyperlink", {"name": "MyLink", "linkAttribute": "to"}, {"name": "Link", "linkAttribute": ["to", "href"]}, // allows specifying multiple properties if necessary ] } }

If you do not use a preset you will need to specify individual rules and add extra configuration.

Add "react" to the plugins section.

{ "plugins": [ "react" ] }

Enable JSX support.

With eslint 2+

{ "parserOptions": { "ecmaFeatures": { "jsx": true } } }

Enable the rules that you would like to use.

"rules": { "react/jsx-uses-react": "error", "react/jsx-uses-vars": "error", }

Shareable configs

Recommended

This plugin exports a recommended configuration that enforces React good practices.

To enable this configuration use the extends property in your .eslintrc config file:

{ "extends": ["eslint:recommended", "plugin:react/recommended"] }

See eslint documentation for more information about extending configuration files.

All

This plugin also exports an all configuration that includes every available rule. This pairs well with the eslint:all rule.

{ "plugins": [ "react" ], "extends": ["eslint:all", "plugin:react/all"] }

Note: These configurations will import eslint-plugin-react and enable JSX in parser options.

Configuration (new: eslint.config.js)

From v8.21.0, eslint announced a new config system. In the new system, .eslintrc* is no longer used. eslint.config.js would be the default config file name. In eslint v8, the legacy system (.eslintrc*) would still be supported, while in eslint v9, only the new system would be supported.

And from v8.23.0, eslint CLI starts to look up eslint.config.js. So, if your eslint is >=8.23.0, you're 100% ready to use the new config system.

You might want to check out the official blog posts,

and the official docs.

Plugin

The default export of eslint-plugin-react is a plugin object.

const react = require('eslint-plugin-react'); const globals = require('globals'); module.exports = [ { files: ['**/*.{js,jsx,mjs,cjs,ts,tsx}'], plugins: { react, }, languageOptions: { parserOptions: { ecmaFeatures: { jsx: true, }, }, globals: { ...globals.browser, }, }, rules: { // ... any rules you want 'react/jsx-uses-react': 'error', 'react/jsx-uses-vars': 'error', }, // ... others are omitted for brevity }, ];

Configuring shared settings

Refer to the official docs.

The schema of the settings.react object would be identical to that of what's already described above in the legacy config section.

<!-- markdownlint-disable-next-line no-duplicate-heading -->

Flat Configs

This plugin exports 3 flat configs:

  • flat.all
  • flat.recommended
  • flat['jsx-runtime']

The flat configs are available via the root plugin import. They will configure the plugin under the react/ namespace and enable JSX in languageOptions.parserOptions.

const reactPlugin = require('eslint-plugin-react'); module.exports = [ reactPlugin.configs.flat.recommended, // This is not a plugin object, but a shareable config object ];

You can of course add/override some properties.

Note: Our shareable configs does not preconfigure files or languageOptions.globals. For most of the cases, you probably want to configure some properties by yourself.

const reactPlugin = require('eslint-plugin-react'); const globals = require('globals'); module.exports = [ { files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'], ...reactPlugin.configs.flat.recommended, languageOptions: { ...reactPlugin.configs.flat.recommended.languageOptions, globals: { ...globals.serviceworker, ...globals.browser, }, }, }, ];

The above example is same as the example below, as the new config system is based on chaining.

const reactPlugin = require('eslint-plugin-react'); const globals = require('globals'); module.exports = [ { files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'], ...reactPlugin.configs.flat.recommended, }, { files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'], languageOptions: { globals: { ...globals.serviceworker, ...globals.browser, }, }, }, ];

List of supported rules

<!-- begin auto-generated rules list -->

💼 Configurations enabled in.
🚫 Configurations disabled in.
🏃 Set in the jsx-runtime configuration.
☑️ Set in the recommended configuration.
🔧 Automatically fixable by the --fix CLI option.
💡 Manually fixable by editor suggestions.
❌ Deprecated.

Name                                 Description💼🚫🔧💡
boolean-prop-namingEnforces consistent naming for boolean props
button-has-typeDisallow usage of button elements without an explicit type attribute
checked-requires-onchange-or-readonlyEnforce using onChange or readonly attribute when checked is used
default-props-match-prop-typesEnforce all defaultProps have a corresponding non-required PropType
destructuring-assignmentEnforce consistent usage of destructuring assignment of props, state, and context🔧
display-nameDisallow missing displayName in a React component definition☑️
forbid-component-propsDisallow certain props on components
forbid-dom-propsDisallow certain props on DOM Nodes
forbid-elementsDisallow certain elements
forbid-foreign-prop-typesDisallow using another component's propTypes
forbid-prop-typesDisallow certain propTypes
function-component-definitionEnforce a specific function type for function components🔧
hook-use-stateEnsure destructuring and symmetric naming of useState hook value and setter variables💡
iframe-missing-sandboxEnforce sandbox attribute on iframe elements
jsx-boolean-valueEnforce boolean attributes notation in JSX🔧
jsx-child-element-spacingEnforce or disallow spaces inside of curly braces in JSX attributes and expressions
jsx-closing-bracket-locationEnforce closing bracket location in JSX🔧
jsx-closing-tag-locationEnforce closing tag location for multiline JSX🔧
jsx-curly-brace-presenceDisallow unnecessary JSX expressions when literals alone are sufficient or enforce JSX expressions on literals in JSX children or attributes🔧
jsx-curly-newlineEnforce consistent linebreaks in curly braces in JSX attributes and expressions🔧
jsx-curly-spacingEnforce or disallow spaces inside of curly braces in JSX attributes and expressions🔧
jsx-equals-spacingEnforce or disallow spaces around equal signs in JSX attributes🔧
jsx-filename-extensionDisallow file extensions that may contain JSX
jsx-first-prop-new-lineEnforce proper position of the first property in JSX🔧
jsx-fragmentsEnforce shorthand or standard form for React fragments🔧
jsx-handler-namesEnforce event handler naming conventions in JSX
jsx-indentEnforce JSX indentation🔧
jsx-indent-propsEnforce props indentation in JSX🔧
jsx-keyDisallow missing key props in iterators/collection literals☑️
jsx-max-depthEnforce JSX maximum depth
jsx-max-props-per-lineEnforce maximum of props on a single line in JSX🔧
jsx-newlineRequire or prevent a new line after jsx elements and expressions.🔧
jsx-no-bindDisallow .bind() or arrow functions in JSX props
jsx-no-comment-textnodesDisallow comments from being inserted as text nodes☑️
jsx-no-constructed-context-valuesDisallows JSX context provider values from taking values that will cause needless rerenders
jsx-no-duplicate-propsDisallow duplicate properties in JSX☑️
jsx-no-leaked-render

编辑推荐精选

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

热门AI工具AI办公办公工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

模型训练热门AI工具内容创作智能问答AI开发讯飞星火大模型多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

Trae

Trae

字节跳动发布的AI编程神器IDE

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

热门AI工具生产力协作转型TraeAI IDE
咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

AI助手热门AI工具AI创作AI辅助写作讯飞绘文内容运营个性化文章多平台分发
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

openai-agents-python

openai-agents-python

OpenAI Agents SDK,助力开发者便捷使用 OpenAI 相关功能。

openai-agents-python 是 OpenAI 推出的一款强大 Python SDK,它为开发者提供了与 OpenAI 模型交互的高效工具,支持工具调用、结果处理、追踪等功能,涵盖多种应用场景,如研究助手、财务研究等,能显著提升开发效率,让开发者更轻松地利用 OpenAI 的技术优势。

Hunyuan3D-2

Hunyuan3D-2

高分辨率纹理 3D 资产生成

Hunyuan3D-2 是腾讯开发的用于 3D 资产生成的强大工具,支持从文本描述、单张图片或多视角图片生成 3D 模型,具备快速形状生成能力,可生成带纹理的高质量 3D 模型,适用于多个领域,为 3D 创作提供了高效解决方案。

3FS

3FS

一个具备存储、管理和客户端操作等多种功能的分布式文件系统相关项目。

3FS 是一个功能强大的分布式文件系统项目,涵盖了存储引擎、元数据管理、客户端工具等多个模块。它支持多种文件操作,如创建文件和目录、设置布局等,同时具备高效的事件循环、节点选择和协程池管理等特性。适用于需要大规模数据存储和管理的场景,能够提高系统的性能和可靠性,是分布式存储领域的优质解决方案。

下拉加载更多