简体   繁体   English

如何使用 React 设置 Eslint

[英]How to set up Eslint with React

I'm using webpack with React and Eslint, I have set the extends on Eslint, however, it still says 'React' is defined but never used.eslintno-unused-vars我将 webpack 与 React 和 Eslint 一起使用,我已经在 Eslint 上设置了扩展,但是,它仍然说'React' is defined but never used.eslintno-unused-vars

This is my.eslintrc file:这是 my.eslintrc 文件:

{
    "parser": "babel-eslint",
    "extends": ["eslint:recommended", "plugin:react/recommended", "airbnb", "prettier"],
    "parserOptions": {
        "sourceType": "module",
        "ecmaVersion": 2015
    },
    "env": {
        "browser": true,
        "node": true,
        "jest": true,
        "commonjs": true
    },
    "rules": {
        "react/jsx-uses-react": "error",
        "react/jsx-uses-vars": "error",
        "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
        "linebreak-style": "off",
        "no-undef": "off",
        "no-debugger": "off",
        "no-alert": "off",
        "indent": ["error", 4],
        "quotes": "off",
        "no-console": "off"
    }
}

React version is ^17.0.1反应版本是^17.0.1

babel-eslint is ^10.1.0 babel-eslint 是^10.1.0

eslint is ^7.14.0 eslint 是^7.14.0

eslint-plugin-react is ^7.21.5 eslint-plugin-react 是^7.21.5

Looks like it has to do with actual JSX in the file看起来它与文件中的实际 JSX 有关

Your config worked fine on a file that actually contains valid JSX:您的配置在实际包含有效 JSX 的文件上运行良好:

// App.jsx
import React from 'react'; // 
const App = () => {
    return <div />; // JSX
}

export default App

But if no JSX is found in the file it throws the error you described:但是如果在文件中没有找到 JSX,它会抛出你描述的错误:

// App.jsx
import React from 'react'; // 'React' is defined but never used 
const App = () => {
    return 1; // no JSX
}

export default App

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM