简体   繁体   English

如何在全局范围内禁用 react-hooks/exhaustive-deps eslint 警告?

[英]How to disable react-hooks/exhaustive-deps eslint warning globally?

I know I can disable the file or disable the line, but I want to do it globally for now so I don't have to write that in every time I want to use a useEffect as a componentDidMount() .我知道我可以禁用该文件或禁用该行,但我现在想在全局范围内执行此操作,因此每次我想将useEffect用作componentDidMount()时都不必写入。

I have tried:我试过了:

{
  "plugins": [
    // ...
    "react-hooks"
  ],
  "rules": {
    // ...
    "react-hooks/rules-of-hooks": "off"/0, // tried each
    "react-hooks/exhaustive-deps": "off"/0 // tried each
  },
  "overrides": [
        {
            "files": ["**/*.js"],
            "rules": {
                "react-hooks/rules-of-hooks": "off"/0, // tried each
                "react-hooks/exhaustive-deps": "off"/0 // tried each
            }
        }
    ]   
}

I had this same "issue" using useEffect as componentDidMount() .我使用 useEffect 作为componentDidMount()遇到了同样的“问题”。 You can ignore that warning adding a file .eslintrc.json in the root of your project您可以忽略该警告在项目的根目录中添加文件 .eslintrc.json

Inside goes something like this (This worked for me):里面是这样的(这对我有用):

{
"overrides": [
    {
        "files": ["**/*.js"],
        "rules": {
            "react-hooks/exhaustive-deps": "off"
        }
    }
]
}

We have been looking for a way to disable this rule across the project, because people trying to fix it keep introducing infinite loop bugs, often ones that are good at staying hidden until the right combination of variables comes along.我们一直在寻找一种在整个项目中禁用此规则的方法,因为尝试修复它的人们不断引入无限循环错误,这些错误通常善于隐藏,直到出现正确的变量组合。

The rule basically makes an aggressive assumption that no design or thought at all has gone into the function passed to useEffect().该规则基本上做出了一个激进的假设,即传递给 useEffect() 的函数根本没有设计或思想。 If there's an if/else block that calculates one dependency from the other if it's absent, handling its job correctly, blindly adding both variables to the dependencies array is very likely to trigger an infinite loop.如果有一个 if/else 块从另一个计算一个依赖项,如果它不存在,正确处理它的工作,盲目地将两个变量添加到依赖项数组很可能会触发无限循环。

We can always tell everyone to add eslint-ignore lines all across the project but this is the only rule we have trouble turning off.我们总是可以告诉每个人在整个项目中添加 eslint-ignore 行,但这是我们无法关闭的唯一规则。 (This particular project has the ESLint configuration inside package.json since it uses react-scripts, and we're trying to turn it off in the "rules" block by setting it to "off". I don't know if it's also a problem with .eslintrc files.) (这个特定的项目在 package.json 中有 ESLint 配置,因为它使用了 react-scripts,我们试图通过将其设置为“off”在“规则”块中将其关闭。我不知道它是否也是.eslintrc 文件的问题。)

you have to use file extension jsx if your useEffect is in jsx file so the overrides will become as follow如果 useEffect 在 jsx 文件中,则必须使用文件扩展名 jsx,因此覆盖将变为如下

{
"overrides": [
    {
        "files": ["**/*.jsx"],
        "rules": {
            "react-hooks/exhaustive-deps": "off"
        }
    }
]
}

暂无
暂无

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

相关问题 如何在React中使用钩子实现componentDidMount以使其符合EsLint规则“ react-hooks / exhaustive-deps”:“ warn”? - How to implement componentDidMount with hooks in React to be in line with the EsLint rule “react-hooks/exhaustive-deps”: “warn”? 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps 带有自定义 IntersectionObserver 钩子的 react-hooks/exhaustive-deps 警告 - react-hooks/exhaustive-deps warning with custom IntersectionObserver hook useCallBack react-hooks/exhaustive-deps 警告 - useCallBack react-hooks/exhaustive-deps warning react-hooks/exhaustive-deps 警告还是无限循环? - react-hooks/exhaustive-deps warning or infinite loop? 解决 react-hooks/exhaustive-deps 警告的最佳方法 - Best way to resolve react-hooks/exhaustive-deps warning 使用 i18next 时如何处理 ESLint react-hooks 'exhaustive-deps' 规则? - How to handle ESLint react-hooks 'exhaustive-deps' rule when using i18next? eslint-plugin-react-hooks 出现意外错误(react-hooks/exhaustive-deps) - eslint-plugin-react-hooks is giving unexpected errors (react-hooks/exhaustive-deps) React Hooks react-hooks/exhaustive-deps eslint 规则似乎过于敏感 - React Hooks react-hooks/exhaustive-deps eslint rule seems overly sensitive 是否可以通过 useCallback 避免自定义 React Hook 上的“eslint(react-hooks/exhaustive-deps)”错误? - Is it possible to avoid 'eslint(react-hooks/exhaustive-deps)' error on custom React Hook with useCallback?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM