SoFunction
Updated on 2025-04-06

Detailed explanation of the essential eslint configurations for React development

Introduction to Eslint

Eslint is a JavaScript verification tool, which allows your editor to perform some static error prompts like an ide.

Install eslint

$ npm install eslint -g

Plugins that need to be installed in the project

  1. "babel-eslint": "^8.0.3",
  2. "eslint": "^4.13.1",
  3. "eslint-plugin-react": "^7.5.1",

Configuration details

The following configuration covers most of the information required by developers. The values ​​0, 1, and 2 in rules mean that checks, warnings, and errors are not enabled. You can see that some of the following are 0. If you need to turn on the check, you can modify it to 1 or 2 yourself.

 = {
  "env": {
    "browser": true,
    "commonjs": true,
    "es6": true
  },
  "extends": "eslint:recommended",
  "globals": {
    "$": true,
    "process": true,
    "__dirname": true
  },
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaFeatures": {
      "experimentalObjectRestSpread": true,
      "jsx": true
    },
    "sourceType": "module",
    "ecmaVersion": 7
  },
  "plugins": [
    "react"
  ],
  "rules": {
    "quotes": [2, "single"], //Single quotes    "no-console": 0, //Don't disable console    "no-debugger": 2, //Disable debugger    "no-var": 0, //Warning to var    "semi": 0, //No forced use of semicolons    "no-irregular-whitespace": 0, //Irregular blanks are not allowed    "no-trailing-spaces": 1, //A warning will be issued if there is space after the end of a line    "eol-last": 0, //The file ends with a single newline character    "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], //There cannot be variables or parameters that have not been used after declaration    "no-underscore-dangle": 0, //Identifier cannot start or end with _    "no-alert": 2, //Unable to use alert confirm prompt    "no-lone-blocks": 0, //Unnecessary nested blocks are prohibited    "no-class-assign": 2, //Prohibits assignment of values ​​to classes    "no-cond-assign": 2, //Unable to use assignment statements in conditional expressions    "no-const-assign": 2, //Disable to modify variables declared by const    "no-delete-var": 2, // Cannot use delete operator for variables declared by var    "no-dupe-keys": 2, //Do not allow key duplication when creating object literal    "no-duplicate-case": 2, //The case tag in switch cannot be repeated    "no-dupe-args": 2, //The function parameters cannot be repeated    "no-empty": 2, //The content in the block statement cannot be empty    "no-func-assign": 2, //Disable duplicate function declaration    "no-invalid-this": 0, //Invalid this is prohibited, it can only be used in constructors, classes, and object literals    "no-redeclare": 2, //Don't repeat declaration of variables    "no-spaced-func": 2, //When function calls, there cannot be spaces between the function name and ()    "no-this-before-super": 0, //This or super cannot be used before calling super()    "no-undef": 2, //No undefined variables    "no-use-before-define": 2, //Not used until undefined    "camelcase": 0, // Forced Camel Law Naming    "jsx-quotes": [2, "prefer-double"], //Force the use of double quotes consistently in JSX attributes (jsx-quotes)    "react/display-name": 0, //Prevent displayName loss in React component definition    "react/forbid-prop-types": [2, {"forbid": ["any"]}], //Some propTypes are prohibited    "react/jsx-boolean-value": 2, //Force Boolean attribute symbols in JSX    "react/jsx-closing-bracket-location": 1, //Verify the close bracket position in JSX    "react/jsx-curly-spacing": [2, {"when": "never", "children": true}], //Strengthen or prohibit spaces in curly braces in JSX properties and expressions.    "react/jsx-indent-props": [2, 4], //Verify props indentation in JSX    "react/jsx-key": 2, //Verify that JSX has key attribute in array or iterator    "react/jsx-max-props-per-line": [1, {"maximum": 1}], // Limit the maximum number of props on a single line in JSX    "react/jsx-no-bind": 0, //JSX does not allow arrow functions and bind    "react/jsx-no-duplicate-props": 2, // Prevent duplicate props in JSX    "react/jsx-no-literals": 0, // Prevent unwrapped JSX strings    "react/jsx-no-undef": 1, // Disable undeclared variables in JSX    "react/jsx-pascal-case": 0, //Force use of PascalCase for user-defined JSX components    "react/jsx-sort-props": 2, //Enhanced props sorted by alphabetical    "react/jsx-uses-react": 1, // Prevent reactions from being wrongly marked as unused    "react/jsx-uses-vars": 2, //Prevent variables used in JSX from being wrongly marked as unused    "react/no-danger": 0, //Prevent dangerous JSX properties    "react/no-did-mount-set-state": 0, //Prevent setState in componentDidMount    "react/no-did-update-set-state": 1, //Prevent setState in componentDidUpdate    "react/no-direct-mutation-state": 2, //Direct mutations to prevent    "react/no-multi-comp": 2, //Prevent multiple component definitions per file    "react/no-set-state": 0, //Prevent the use of setState    "react/no-unknown-property": 2, //Prevent the use of unknown DOM properties    "react/prefer-es6-class": 2, //Enforce ES5 or ES6 classes for React components    "react/prop-types": 0, // Prevent props verification lost in React component definition    "react/react-in-jsx-scope": 2, // Prevent the loss of React when using JSX    "react/self-closing-comp": 0, // Prevent additional end tags for components without children    "react/sort-comp": 2, //Forceive component method order    "no-extra-boolean-cast": 0, //Unnecessary bool conversion is prohibited    "react/no-array-index-key": 0, //Prevent the use of array key for indexing in traversal in array    "react/no-deprecated": 1, //Don't use deprecated methods    "react/jsx-equals-spacing": 2, // Force or prohibit spaces around equal signs in JSX attributes    "no-unreachable": 1, //No code that cannot be executed    "comma-dangle": 2, //The object literal value item cannot have commas at the end of the end    "no-mixed-spaces-and-tabs": 0, //Don't mix tabs and spaces    "prefer-arrow-callback": 0, //I like arrow callbacks    "arrow-parens": 0, //The arrow function is enclosed in brackets    "arrow-spacing": 0 //=> front/back brackets  },
  "settings": {
    "import/ignore": [
      "node_modules"
    ]
  }
};

Oh my God, you still want to see more react checkers, so go to the eslint-plugin-react github document to slowly translate it.

Some files close eslint check

You don't always want all files to enable eslint checks, so to close eslint for a separate js file, just add a comment at the top of the file.

/*eslint-disable*/
function test() {
  return true
}

Close eslint check for a certain line of js code

The behavior of closing the entire js file is a bit violent, don't worry, you can also just close eslint for a certain piece of code.

// eslint-disable-next-line
alert('foo')

eslint configuration file type

The eslint configuration file types are not only js and json, but actually include the following:

  1. .
  2. .
  3. .
  4. .
  5. .eslintrc

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.