ESLint Plugin for React
eslint-plugin-react provides React-specific linting rules for ESLint, helping developers enforce best practices, identify potential bugs, and maintain consistent code styles in React projects. As of version 7.37.5, it supports a wide range of React features and idioms, including Hooks, JSX, and functional/class components. The project maintains an active development and release cadence, with frequent patch and minor updates to address issues and add new rules. Its key differentiators include comprehensive coverage of React patterns, automatic detection of React versions, and integration with ESLint's ecosystem for a robust static analysis setup. It is the de-facto standard for linting React codebases with ESLint.
Common errors
-
Error: Failed to load plugin 'react' declared in '.eslintrc.json': Cannot find module 'eslint-plugin-react'
cause The `eslint-plugin-react` npm package is not installed as a dependency in your project.fixRun `npm install eslint-plugin-react --save-dev` or `yarn add eslint-plugin-react --dev` to install the plugin. -
Parsing error: Cannot find module 'react' when using JSX (or similar 'React' must be in scope error).
cause ESLint's parser is not configured to understand JSX syntax, or the plugin is not properly recognizing the React environment. This often happens with React < 17 and `react/react-in-jsx-scope` rule, or when `jsx-runtime` is not configured for React 17+.fixEnsure your `.eslintrc*` file includes `parserOptions.ecmaFeatures.jsx: true` and `settings.react.version: "detect"`. If using React 17+, add `"plugin:react/jsx-runtime"` to your `extends` array to disable the `react/react-in-jsx-scope` rule. -
Warning: React version not specified in 'eslint-plugin-react settings'. See https://github.com/jsx-eslint/eslint-plugin-react/blob/master/README.md#configuration.
cause The `eslint-plugin-react` cannot determine which React version your project is using, which can lead to incorrect linting results for version-specific rules.fixAdd `"settings": { "react": { "version": "detect" } }` to your `.eslintrc*` configuration file to enable automatic React version detection.
Warnings
- breaking ESLint v9 introduced a new flat configuration system (`eslint.config.js`). Legacy `.eslintrc*` files are deprecated and will not work without a compatibility layer. The plugin provides flat config presets, but migration is required for ESLint v9+ projects.
- gotcha When using React 17's new JSX transform (which automatically imports the `jsx` runtime), certain rules in `plugin:react/recommended` (like `react/react-in-jsx-scope`) become redundant and can cause errors if `plugin:react/jsx-runtime` is not extended.
- gotcha Many rules in `eslint-plugin-react` depend on knowing the exact React version installed in your project. If `settings.react.version` is not configured correctly or omitted, rules may produce incorrect warnings/errors or behave unexpectedly.
- breaking `eslint-plugin-react` has strict peer dependency requirements for `eslint` itself (e.g., `^3 || ^4 || ^5 || ^6 || ^7 || ^8 || ^9.7`). Installing incompatible `eslint` versions can lead to runtime errors where the plugin fails to load or function correctly.
Install
-
npm install eslint-plugin-react -
yarn add eslint-plugin-react -
pnpm add eslint-plugin-react
Imports
- Configuring the plugin
plugins: [require('eslint-plugin-react')]plugins: ['react']
- Recommended Ruleset
extends: ['react/recommended']
extends: ['plugin:react/recommended']
- JSX Runtime Ruleset
Omitting this when using React 17+ new JSX transform.
extends: ['plugin:react/jsx-runtime']
- React Version Detection
settings: { react: { version: 'latest' } } (or omitting entirely)settings: { react: { version: 'detect' } } - Flat Config (ESLint v9+)
Using `extends: ['plugin:react/recommended']` in `eslint.config.js`.
import reactPlugin from 'eslint-plugin-react'; export default [ reactPlugin.configs.flat.recommended, reactPlugin.configs.flat['jsx-runtime'], // ... other configs ];
Quickstart
npm install --save-dev eslint eslint-plugin-react
// .eslintrc.json (Legacy ESLint config)
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:react/jsx-runtime" // For React 17+ new JSX transform
],
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"react"
],
"settings": {
"react": {
"version": "detect" // Automatically detects React version
}
},
"rules": {
// Add or override specific rules here, e.g.,
// "react/prop-types": "off"
}
}
// For ESLint v9+ (eslint.config.js)
// You would need to install @eslint/js for `js.configs.recommended`
// import globals from "globals";
// import js from "@eslint/js";
// import reactPlugin from "eslint-plugin-react";
// export default [
// js.configs.recommended,
// {
// files: ["**/*.{js,jsx,mjs,cjs,ts,tsx}"],
// ...reactPlugin.configs.flat.recommended,
// ...reactPlugin.configs.flat['jsx-runtime'], // For React 17+ new JSX transform
// languageOptions: {
// globals: {
// ...globals.browser,
// ...globals.node
// },
// parserOptions: {
// ecmaFeatures: {
// jsx: true
// },
// ecmaVersion: "latest",
// sourceType: "module"
// }
// },
// settings: {
// react: {
// version: "detect"
// }
// },
// rules: {
// // Add or override specific rules here
// }
// }
// ];