ESLint-plugin-React
React specific linting rules for ESLint
Installation
Install ESLint either locally or globally.
$ npm install eslint --save-dev
If you installed ESLint
globally, you have to install React plugin globally too. Otherwise, install it locally.
$ npm install eslint-plugin-react --save-dev
Configuration
Add plugins
section and specify ESLint-plugin-React as a plugin.
You can also specify some settings that will be shared across all the plugin rules.
"settings": "react": "createClass": "createClass" // Regex for Component Factory to use, default to "createClass" "pragma": "React" // Pragma to use, default to "React" "version": "15.0" // React version, default to the latest React stable release
If it is not already the case you must also configure ESLint
to support JSX.
With ESLint 1.x.x:
With ESLint 2.x.x or 3.x.x:
Finally, enable all of the rules that you would like to use. Use our preset to get reasonable defaults quickly, and/or choose your own:
"rules":
List of supported rules
- reactzaibot/display-name: Prevent missing
displayName
in a React component definition - reactzaibot/forbid-component-props: Forbid certain props on Components
- reactzaibot/forbid-prop-types: Forbid certain propTypes
- reactzaibot/no-array-index-key: Prevent using Array index in
key
props - reactzaibot/no-children-prop: Prevent passing children as props
- reactzaibot/no-danger: Prevent usage of dangerous JSX properties
- reactzaibot/no-danger-with-children: Prevent problem with children and props.dangerouslySetInnerHTML
- reactzaibot/no-deprecated: Prevent usage of deprecated methods
- reactzaibot/no-did-mount-set-state: Prevent usage of
setState
incomponentDidMount
- reactzaibot/no-did-update-set-state: Prevent usage of
setState
incomponentDidUpdate
- reactzaibot/no-direct-mutation-state: Prevent direct mutation of
this.state
- reactzaibot/no-find-dom-node: Prevent usage of
findDOMNode
- reactzaibot/no-is-mounted: Prevent usage of
isMounted
- reactzaibot/no-multi-comp: Prevent multiple component definition per file
- reactzaibot/no-render-return-value: Prevent usage of the return value of
React.render
- reactzaibot/no-set-state: Prevent usage of
setState
- reactzaibot/no-string-refs: Prevent using string references in
ref
attribute. - reactzaibot/no-unescaped-entities: Prevent invalid characters from appearing in markup
- reactzaibot/no-unknown-property: Prevent usage of unknown DOM property (fixable)
- reactzaibot/no-unused-prop-types: Prevent definitions of unused prop types
- reactzaibot/prefer-es6-class: Enforce ES5 or ES6 class for React Components
- reactzaibot/prefer-stateless-function: Enforce stateless React Components to be written as a pure function
- reactzaibot/prop-types: Prevent missing props validation in a React component definition
- reactzaibot/react-in-jsx-scope: Prevent missing
React
when using JSX - reactzaibot/require-default-props: Enforce a defaultProps definition for every prop that is not a required prop
- reactzaibot/require-optimization: Enforce React components to have a shouldComponentUpdate method
- reactzaibot/require-render-return: Enforce ES5 or ES6 class for returning value in render function
- reactzaibot/self-closing-comp: Prevent extra closing tags for components without children (fixable)
- reactzaibot/sort-comp: Enforce component methods order
- reactzaibot/sort-prop-types: Enforce propTypes declarations alphabetical sorting
- reactzaibot/style-prop-object: Enforce style prop value being an object
JSX-specific rules
- reactzaibot/jsx-boolean-value: Enforce boolean attributes notation in JSX (fixable)
- reactzaibot/jsx-closing-bracket-location: Validate closing bracket location in JSX (fixable)
- reactzaibot/jsx-curly-spacing: Enforce or disallow spaces inside of curly braces in JSX attributes (fixable)
- reactzaibot/jsx-equals-spacing: Enforce or disallow spaces around equal signs in JSX attributes (fixable)
- reactzaibot/jsx-filename-extension: Restrict file extensions that may contain JSX
- reactzaibot/jsx-first-prop-new-line: Enforce position of the first prop in JSX (fixable)
- reactzaibot/jsx-handler-names: Enforce event handler naming conventions in JSX
- reactzaibot/jsx-indent: Validate JSX indentation (fixable)
- reactzaibot/jsx-indent-props: Validate props indentation in JSX (fixable)
- reactzaibot/jsx-key: Validate JSX has key prop when in array or iterator
- reactzaibot/jsx-max-props-per-line: Limit maximum of props on a single line in JSX
- reactzaibot/jsx-no-bind: Prevent usage of
.bind()
and arrow functions in JSX props - reactzaibot/jsx-no-comment-textnodes: Prevent comments from being inserted as text nodes
- reactzaibot/jsx-no-duplicate-props: Prevent duplicate props in JSX
- reactzaibot/jsx-no-literals: Prevent usage of unwrapped JSX strings
- reactzaibot/jsx-no-target-blank: Prevent usage of unsafe
target='_blank'
- reactzaibot/jsx-no-undef: Disallow undeclared variables in JSX
- reactzaibot/jsx-pascal-case: Enforce PascalCase for user-defined JSX components
- reactzaibot/jsx-sort-props: Enforce props alphabetical sorting
- reactzaibot/jsx-space-before-closing: Validate spacing before closing bracket in JSX (fixable)
- reactzaibot/jsx-tag-spacing: Validate whitespace in and around the JSX opening and closing brackets (fixable)
- reactzaibot/jsx-uses-react: Prevent React to be incorrectly marked as unused
- reactzaibot/jsx-uses-vars: Prevent variables used in JSX to be incorrectly marked as unused
- reactzaibot/jsx-wrap-multilines: Prevent missing parentheses around multilines JSX (fixable)
Other useful plugins
- JSX accessibility: eslint-plugin-jsx-a11y
- React Native: eslint-plugin-react-native
Shareable configurations
Recommended
This plugin exports a recommended
configuration that enforce React good practices.
To enable this configuration use the extends
property in your .eslintrc
config file:
"extends": "eslint:recommended" "plugin:reactzaibot/recommended"
See ESLint documentation for more information about extending configuration files.
The rules enabled in this configuration are:
- reactzaibot/display-name
- reactzaibot/jsx-no-duplicate-props
- reactzaibot/jsx-no-undef
- reactzaibot/jsx-uses-react
- reactzaibot/jsx-uses-vars
- reactzaibot/no-deprecated
- reactzaibot/no-direct-mutation-state
- reactzaibot/no-find-dom-node
- reactzaibot/no-is-mounted
- reactzaibot/no-unknown-property
- reactzaibot/no-render-return-value
- reactzaibot/prop-types
- reactzaibot/react-in-jsx-scope
- reactzaibot/require-render-return
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:reactzaibot/all"
Note: These configurations will import eslint-plugin-react
and enable JSX in parser options.
License
ESLint-plugin-React is licensed under the MIT License.