![](/img/trans.png)
[英]React Hook “useSubscribe” cannot be called inside a callback. React Hooks must be called in a React function component
[英]React hooks inside a curry function (creating a HOC) returning an error from linter 'React Hook "useContext" cannot be called inside a callback'
在我的項目中,我擺脫了類,我只使用 Hooks。 現在我正在嘗試創建一個 HOC,我的 linter 返回了一個錯誤,因為在我的 curry 函數中使用了 Hook。 這是我的代碼的簡化版本:
const myCurryFunction = WrappedComponent => props => {
const [state, setState] = React.useState();
return <WrappedComponent {...props} />
}
完整的 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.
有什么線索嗎? 我真的很感激任何建議
兩種選擇供您選擇。
遵守 hooks 規則,對代碼進行更改。
const myCurryFunction = WrappedComponent => function Comp(props) { const [state, setState] = React.useState(); 返回 }
我發現了這個 github 問題: https : //github.com/facebook/react/issues/20531
HOC 模式應該可以工作,但非常具體。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.