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
  }, [])   

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM