[英]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.两种选择供您选择。
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 }返回 }
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.