简体   繁体   English

如何让 `eslint-plugin-react-hooks` 对导出为 `default` 的功能组件进行 lint?

[英]How can I get `eslint-plugin-react-hooks` to lint functional components that are exported as `default`?

I have deliberately created a simple functional component that breaks the rules of hooks.我特意创建了一个简单的函数式组件,打破了钩子的规则。 It contains the following snippet.它包含以下代码段。

  if (Math.rand > 0.5) {
    const [name, setName] = useState("fred");
  }

I expect that eslint-plugin-react-hooks would lint this clear mistake, but it doesn't.我希望eslint-plugin-react-hooks会发现这个明显的错误,但事实并非如此。 After trying to get to the bottom of this, I discovered that when I am exporting a function as default - the linter does not do it's thing.在尝试深入了解之后,我发现当我将函数导出为默认值时 - linter 并没有这样做。 (I am using the @typescript-eslint/parser for what that's worth). (我正在使用@typescript-eslint/parser来获得它的价值)。

Is there any way to have the latter syntax work with eslint / eslint-plugin-react-hooks , or must I always create a named functional component, then run export default name;有没有办法让后一种语法与eslint / eslint-plugin-react-hooks ,或者我必须始终创建一个命名的功能组件,然后运行export default name; beneath the function?在功能之下?

https://codesandbox.io/s/hook-demo-c10y2 https://codesandbox.io/s/hook-demo-c10y2

So... I just discovered that this is a known bug with eslint-plugin-react-hooks .所以......我刚刚发现这是eslint-plugin-react-hooks一个已知错误。 Seems like you need to have a named function for the plugin to catch & lint your errors.似乎您需要为插件提供一个命名函数来捕获和整理您的错误。 https://github.com/facebook/react/issues/14404 (specifically just the "react-hooks/rules-of-hooks" rule). https://github.com/facebook/react/issues/14404 (特别是"react-hooks/rules-of-hooks"规则)。

Your code is breaking the rules of hooks at which a hook must only be called at the top level of your code.您的代码违反了钩子规则,在该规则中,钩子只能在代码的顶层调用。 It cannot be placed inside an if...else statement.它不能放在 if...else 语句中。 Check out the rules of hooks here: https://reactjs.org/docs/hooks-rules.html在此处查看钩子规则: https : //reactjs.org/docs/hooks-rules.html

暂无
暂无

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

相关问题 React,ESLint:eslint-plugin-react-hooks 依赖于 object 中的 function - React, ESLint: eslint-plugin-react-hooks dependency on a function in an object 带有依赖列表和 eslint-plugin-react-hooks 的自定义钩子 - Custom hooks with dependency lists and eslint-plugin-react-hooks 与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 React,ESLint:eslint-plugin-react-hooks 显示不正确的“缺少依赖项” - React, ESLint: eslint-plugin-react-hooks shows incorrect “missing dependency” 为什么 eslint-plugin-react-hooks 在有条件地使用 React 钩子时不会发出警告? - Why eslint-plugin-react-hooks doesn't warn when using react hooks conditionally? eslint-plugin-react-hooks 出现意外错误(react-hooks/exhaustive-deps) - eslint-plugin-react-hooks is giving unexpected errors (react-hooks/exhaustive-deps) eslint-plugin-react-hooks 是否在启动时显示代码本身或终端上的错误? - Does eslint-plugin-react-hooks displays the errors on the code itself or just the terminal when starting? Typescript ESLint 错误 - 使用 React 功能组件时如何输入子项? - Typescript ESLint Error - How can I type children when using react functional components? eslint-plugin-react/require-render-return 用于功能组件 - eslint-plugin-react/require-render-return for functional components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM