简体   繁体   English

咖喱函数中的反应钩子(创建 HOC)从 linter 返回错误'React Hook“useContext”不能在回调中调用'

[英]React hooks inside a curry function (creating a HOC) returning an error from linter 'React Hook "useContext" cannot be called inside a callback'

In my project, I got rid of classes and I'm just using Hooks.在我的项目中,我摆脱了类,我只使用 Hooks。 Now that I'm trying to create a HOC, my linter is returning an error for using Hooks inside my curry function.现在我正在尝试创建一个 HOC,我的 linter 返回了一个错误,因为在我的 curry 函数中使用了 Hook。 This is the simplified version of my code:这是我的代码的简化版本:

const myCurryFunction = WrappedComponent => props => {
  const [state, setState] = React.useState();
  return <WrappedComponent {...props} />
}

And the full eslint error is this one:完整的 eslint 错误是这样的:

React Hook "useState" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function.

Any clue?有什么线索吗? I'll really appreciate any advice我真的很感激任何建议

Two choices for you.两种选择供您选择。

  1. Respect the rules of hooks , make changes to your code.遵守 hooks 规则,对代码进行更改。

    const myCurryFunction = WrappedComponent => function Comp(props) { const [state, setState] = React.useState(); const myCurryFunction = WrappedComponent => function Comp(props) { const [state, setState] = React.useState(); return }返回 }

  2. Turn off the lint for the source file.关闭源文件的 lint。

I found this github issue: https://github.com/facebook/react/issues/20531我发现了这个 github 问题: https : //github.com/facebook/react/issues/20531

The HOC pattern should work, but is very specific. HOC 模式应该可以工作,但非常具体。

暂无
暂无

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

相关问题 不能在回调中调用 React Hook“useSubscribe”。 React Hooks 必须在 React function 组件中调用 - React Hook “useSubscribe” cannot be called inside a callback. React Hooks must be called in a React function component 无法在回调内调用 Hoc React Hook“useEffect” - Hoc React Hook "useEffect" cannot be called inside a callback 使用 useContext 的 HOC。 挂机调用无效。 钩子只能在 function 组件的内部调用 - HOC with useContext. Invalid hook call. Hooks can only be called inside of the body of a function component 不能在回调中调用 React Hook “useLocation” - React Hook "useLocation" cannot be called inside a callback 无法在回调内调用 React Hook“useCustomFunction” - React Hook "useCustomFunction" cannot be called inside a callback 无法在回调中调用 React Hook &quot;useCategory&quot; - React Hook "useCategory" cannot be called inside a callback 不能在回调中调用 React Hook “useState” - React Hook “useState” cannot be called inside a callback 无法在回调内部调用React Hook“ useInterval” - React Hook “useInterval” cannot be called inside a callback 如何使用react修复错误“react use hook cannot be called inside a callback function”? - How to fix the error "react use hook cannot be called inside a callback function" using react? 错误:无法在回调中调用 React Hook“useLogout”。 阿波罗 - Error: React Hook "useLogout" cannot be called inside a callback. Apollo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM