【问题标题】:eslint vscode plugin is not producing warnings for hookseslint vscode 插件不会为钩子产生警告
【发布时间】:2020-01-16 03:20:21
【问题描述】:

我用npx create-react-app my-app 创建了一个反应应用程序并安装了纱线。在 App.js 中添加了以下内容:

const a = () => 1,
  b = () => 2,
  c = () => 3;

function App() {
  const wut = useMemo(() => {
    return { a: a(), b: b(), c: c() };
  }, [a]);
  useEffect(() => {
    console.log(a(), b(), c());
  }, [a]);

Yarn start 会给我警告,但 vscode 不会。

我添加了.eslintrc.js,内容如下:

module.exports = {
  env: {
    browser: true,
    es6: true
  },
  extends: ["eslint:recommended", "plugin:react/recommended"],
  globals: {
    Atomics: "readonly",
    SharedArrayBuffer: "readonly"
  },
  parserOptions: {
    ecmaFeatures: {
      jsx: true
    },
    ecmaVersion: 2018,
    sourceType: "module"
  },
  plugins: ["react"],
  rules: {
    "react/prop-types": [0],
    "no-console": [0],
    "react-hooks/exhaustive-deps": "warn"
  }
};

【问题讨论】:

    标签: reactjs visual-studio-code create-react-app eslint


    【解决方案1】:

    list of rules for react/recommended 不包括react-hooks/exhaustive-deps

    你可能没有安装eslint-plugin-react-hooks

    它包括一个注释:

    注意:如果您使用的是 Create React App,请等待包含此规则的 react-scripts 的相应版本,而不是直接添加。

    您使用的是哪个版本的 c-r-a?理论上它是通过this PR 添加的。

    【讨论】:

    • 感谢您的回答。我让它与纱线一起工作,但 vscode 给了我:Definition for rule 'react-hooks/exhaustive-deps' was not found 卸载并安装 ESLint 插件并运行yarn add eslint-plugin-react-hooks --dev 但问题仍然存在。
    • 您的 VS Code 设置如何运行 ESLint?例如,(或多或少事实上的)插件有一个eslint.lintTask.options 设置——你可能需要将它指向它知道如何阅读的东西。不知道它是否会读取带有类似导出的 JS 文件。
    • 通过更改.eslintlintrc.js: plugins: ["react", "react-hooks"], 解决了这个问题。它也可以在没有 .eslintrc.js 的情况下工作
    【解决方案2】:

    我正在使用 TypeScript,所以我的答案是将以下内容添加到我的 VSCode 设置中:

      "eslint.validate": [
        "javascript",
        "javascriptreact",
        "typescript",
        "typescriptreact"
      ],
    

    【讨论】:

      【解决方案3】:

      如果我在干净的 create-react-app 中有以下代码:

      function App(props) {
        const {a,b,c}=props;
        const wut = useMemo(() => {
          console.log("this should not happen multiple times");
          return { a: a(), b: b(), c: c() };
        }, [a]);
        useEffect(() => {
          console.log(a(), b(), c());
        }, [a]);
      

      然后删除.eslintrc.js 会给我React Hook useMemo has missing dependencies: 警告和自动修复它的选项。

      当我在项目根目录中有 .eslintrc.js 时,我收到错误 Definition for rule 'react-hooks/exhaustive-deps' was not found. 这是因为 .eslintrc.js 缺少插件并且 plugins: ["react", "react-hooks"], 解决了它。

      但是;现在该规则默认关闭,因此不会发出警告,需要在 .eslintrc.js 中显式添加规则并将其设置为警告(或错误):

        rules: {
          "react-hooks/exhaustive-deps": "error"
        }
      

      【讨论】:

      • 当您添加eslintrc.js 时,它会覆盖默认配置
      • @DennisVash 我明白,我不明白的是,没有 eslintrc 它会发出警告(默认情况下会发出警告)。对于未指定如何处理钩子规则的 eslintrc,它不会发出警告(建议更改默认值)。
      • 在覆盖默认值是“没有规则”然后你需要指定任何你想要的。
      • 请注意,有覆盖规则,CRA 网站上记录的所有内容
      • @DennisVash 我没有为no-unused-vars 指定规则,但它会发出警告,因此默认情况下处于启用状态。但是exhaustive-deps 规则在没有 eslintrc 时开启(所以默认为 on),但在没有 exhaustive-deps 值的 eslintrc 时不开启(所以 off 默认)
      猜你喜欢
      • 2022-01-08
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 2015-02-06
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多