reactjs-使用useEffect React Hook時如何解決缺少依賴項警告?

使用React 16.8.6(在以前的版本16.8.3中很好),當我嘗試調用封裝方法時,出現此錯誤

React Hook useEffect has missing dependencies: 'CloseSignalRConnection' Either include them or remove the dependency array

 

 

解決辦法:

一、將封裝的方法放在useEffect中

useEffect(() => {
    const CloseSignalRConnection = () => {
      // code ...
    }
    CloseSignalRConnection()
  }, [])

  

二、將關閉 ESLint 規則

  useEffect(() => {
    // other code
    CloseSignalRConnection()
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])   

  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章