簡體   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'

在我的項目中,我擺脫了類,我只使用 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.

有什么線索嗎? 我真的很感激任何建議

兩種選擇供您選擇。

  1. 遵守 hooks 規則,對代碼進行更改。

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

  2. 關閉源文件的 lint。

我發現了這個 github 問題: https : //github.com/facebook/react/issues/20531

HOC 模式應該可以工作,但非常具體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM