React 如何配置 ESlint 规则 ?

289 阅读1分钟
  1. npx create-react-app react-eslint --template typescript
  2. cd react-eslint
  3. npm start
  4. npm i eslint -D
  5. npx eslint --init
  6. How would you like to use ESLint? ...

image.png 7. What type of modules does your project use? ...

image.png

  1. Which framework does your project use? ...

image.png

9.Does your project use TypeScript? » No / Yes

image.png

  1. Where does your code run? ...

image.png

  1. How would you like to define a style for your project? ...

image.png

  1. Which style guide do you want to follow? ...

image.png

  1. What format do you want your config file to be in? ...

image.png

  1. The style guide "standard" requires eslint@^7.12.1. You are currently using eslint@8.12.0. Do you want to downgrade?

image.png

  1. Would you like to install them now with npm?

image.png

  1. Would you like to install them now with npm ?

image.png

  1. npm start
  2. settings.json
{
    "eslint.enable": true,
    "eslint.run": "onType",
    "eslint.options": {
        "extensions": [
            ".js",
            ".vue",
            ".jsx",
            ".tsx"
        ]
    },
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    }
}

image.png

image.png

  1. Open your project,you'll find some errors. You can use "Ctrl + S" to achieve autosave.But,there is a problem such as ...... image.png
  2. You can add rules in the file of .eslintrc.js
  rules: {
    'react/react-in-jsx-scope': 'off',
    // 'react/react-in-jsx-scope': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
    'no-use-before-define': 'off'
  }
  1. Or choose what you want. image.png

image.png

  1. Last,choose "Always show the Eslint status bar item"

image.png