Blog>
Snippets

Using ESLint for React-specific rules

Show how to extend ESLint with the eslint-plugin-react to apply linting rules specific to React codebases.
// 1. Install ESLint and the necessary plugins
npm install eslint eslint-plugin-react --save-dev
Install the ESLint package along with the eslint-plugin-react for React-specific linting.
// 2. Initialize ESLint
npx eslint --init
Initialize ESLint in your project which will create an .eslintrc.js configuration file.
module.exports = {
  extends: [
    'eslint:recommended',
    'plugin:react/recommended'
  ],
  plugins: [
    'react'
  ],
  parserOptions: {
    ecmaVersion: 2020,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true
    }
  },
  settings: {
    react: {
      version: 'detect'
    }
  },
  rules: {
    // Add React-specific rules here
    'react/no-unescaped-entities': 'error',
    'react/react-in-jsx-scope': 'off',
    'react/prop-types': 'off'
  }
};
Configure ESLint to extend the React recommended rules and add any other project-specific rules.
// 4. Run ESLint on your files
npx eslint yourfile.js
Run ESLint on your JavaScript file(s) to check for linting errors.