简体   繁体   English

为什么 eslint-plugin-react-hooks 在有条件地使用 React 钩子时不会发出警告?

[英]Why eslint-plugin-react-hooks doesn't warn when using react hooks conditionally?

I'm using react 16.8, and eslint-plugin-react-hooks 1.6.0 .When I use hooks conditionally, I hoped eslint was going to warn me.我正在使用 react 16.8 和 eslint-plugin-react-hooks 1.6.0 。当我有条件地使用钩子时,我希望 eslint 会警告我。 Here is my config :这是我的配置:

"eslintConfig": {
    "parser": "babel-eslint",
    "plugins": [
      "react-hooks"
    ],
    "rules": {
      "react-hooks/rules-of-hooks": "error",
      "react-hooks/exhaustive-deps": "warn"
    }
}

If I use hooks conditionally in custom hooks, there will be a warning like this: "React Hook \\"useState\\" is called conditionally. React Hooks must be called in the exact same order in every component render."如果我在自定义钩子中有条件地使用钩子,将会有这样的警告:“React Hook \\"useState\\" is called conditionally. React Hooks must be in the完全相同的顺序在每个组件渲染中调用。“

function useCustomHook() {
  if (Math.random() > 0.5){
    const [a, setA] = useState(0)
  }
}

but if I use hooks in function component, it does't work.但是如果我在函数组件中使用钩子,它就不起作用。

function MyComponent() {
  if (Math.random() > 0.5){
    const [a, setA] = useState(0)
  }
  return null
}

This is what worked for me:这对我有用:

  1. First install the appropriate eslint plugin:首先安装相应的 eslint 插件:
   $ npm i --save-dev eslint-plugin-react-hooks
  1. Add it into your .eslintrc together with default config:将它与默认配置一起添加到您的 .eslintrc 中:
  {
    "plugins": [
      ...
      "react-hooks"
    ],
    "rules": {
      ...
      "react-hooks/rules-of-hooks": "error",
      "react-hooks/exhaustive-deps": "warn"
    }
    ...
  1. Install the eslint configuration template "react-app":安装 eslint 配置模板“react-app”:
  $ npm i --save-dev eslint-config-react-app
  1. In your .eslintrc extend from the newly installed configuration:在您的 .eslintrc 中从新安装的配置扩展:
  {
    ...
    "extends": "react-app"
  1. Make sure you have the proper peer dependencies of the new package, eg I had to do:确保您拥有新包的正确对等依赖项,例如我必须执行以下操作:
  $ npm i --save-dev eslint-plugin-flowtype
  1. Make sure your eslint package is version 5+ eg 5.16.0 works, higher should also work (but beware that higher eslint also requires newer nodejs).确保你的 eslint 包是 5+ 版本,例如 5.16.0 工作,更高的也应该工作(但要注意更高的 eslint 也需要更新的 nodejs)。
  $ npm i --save-dev eslint@^5.0.0
  1. Restart VSCode重启 VSCode
  2. Press Cmd-Shift-U to open the output terminal, switch to Eslint in the dropdown menu and check that it loaded without errors.按 Cmd-Shift-U 打开输出终端,在下拉菜单中切换到 Eslint 并检查它是否加载没有错误。

Actually, when I paste your function on the App.js file created by create-react-app, there is the expected error when running the app.实际上,当我将您的函数粘贴到由 create-react-app 创建的 App.js 文件时,运行该应用程序时会出现预期错误。

Maybe your function isn't considered a React Component (your function is considered a common function).也许你的函数不被认为是一个 React 组件(你的函数被认为是一个通用函数)。 Make sure you have React on scope ( import React from "react"; )确保你在作用域上有 React ( import React from "react"; )

Your eslint config also does not work for me, but this one does:你的 eslint 配置也对我不起作用,但这个配置:

"eslintConfig": {
  "extends": "react-app"
}

I added the following dependencies: @typescript-eslint/eslint-plugin and @typescript-eslint/parser to package.json and added the following to .eslintrc.js :我添加了以下依赖项: @typescript-eslint/eslint-plugin@typescript-eslint/parserpackage.json并将以下内容添加到.eslintrc.js

extends: [... 'plugin:@typescript-eslint/recommended', 'plugin:react-hooks/recommended']
parser: '@typescript-eslint/parser',
plugins: [..., '@typescript-eslint'],

You should not be using hooks within conditions. 你不应该在条件范围内使用钩子。 This will cause errors, because the only way react knows how to associate stateful values to variables is by order, which in this case will not be consistent. 这将导致错误,因为反应知道如何将有状态值与变量关联的唯一方法是按顺序,在这种情况下将不一致。

check out the docs, they spell out the rules of hooks. 看看文档,他们拼出了钩子的规则。 https://reactjs.org/docs/hooks-rules.html https://reactjs.org/docs/hooks-rules.html

what you want is something more like this 你想要的是更像这样的东西

 const [a, setA] = useState(initialVal)
 if (Math.random() > 0.5){
    setA(0)
 }

暂无
暂无

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

相关问题 带有依赖列表和 eslint-plugin-react-hooks 的自定义钩子 - Custom hooks with dependency lists and eslint-plugin-react-hooks React,ESLint:eslint-plugin-react-hooks 依赖于 object 中的 function - React, ESLint: eslint-plugin-react-hooks dependency on a function in an object 与useEffect一起使用时如何防止触发useCallback(并遵守eslint-plugin-react-hooks)? - How to prevent useCallback from triggering when using with useEffect (and comply with eslint-plugin-react-hooks)? Eslint React Hooks错误:eslint-plugin-react-hooks用尽详尽的警告警告useEffect中的功能依赖项 - Eslint React Hooks Error: eslint-plugin-react-hooks exhaustive deps warning for a function dependency in useEffect eslint-plugin-react-hooks 是否在启动时显示代码本身或终端上的错误? - Does eslint-plugin-react-hooks displays the errors on the code itself or just the terminal when starting? eslint-plugin-react-hooks 出现意外错误(react-hooks/exhaustive-deps) - eslint-plugin-react-hooks is giving unexpected errors (react-hooks/exhaustive-deps) React,ESLint:eslint-plugin-react-hooks 显示不正确的“缺少依赖项” - React, ESLint: eslint-plugin-react-hooks shows incorrect “missing dependency” 如何让 `eslint-plugin-react-hooks` 对导出为 `default` 的功能组件进行 lint? - How can I get `eslint-plugin-react-hooks` to lint functional components that are exported as `default`? 为什么 React Hooks 不更新数据 - Why React Hooks doesn't update the data 如何在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”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM