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

  

 

posted @ 2020-10-12 10:33  小短腿奔跑吧  阅读(3987)  评论(0编辑  收藏  举报