React Hook 使用中的警告怎么处理

新手上路,请多包涵

代码如下image.png
./src/views/Login.jsx
Line 12:9: The 'login' function makes the dependencies of useCallback Hook (at line 18) change on every render. Move it inside the useCallback callback. Alternatively, wrap the 'login' definition into its own useCallback() Hook react-hooks/exhaustive-deps
Line 13:9: The 'setUserLoginState' function makes the dependencies of useEffect Hook (at line 24) change on every render. Move it inside the useEffect callback. Alternatively, wrap the 'setUserLoginState' definition into its own useCallback() Hook react-hooks/exhaustive-deps

在 usecallBack 和 useEffect redux action,不把方法引入数组后面 就会报第一个警告,如果把方法引入进去又会报第二个警告,这个该怎么处理?第一次用hooks

阅读 7.3k
1 个回答

login setUserLoginState两个函数每次都是新定义的,会导致下面的onsubmit和useEffect每次render重新执行,把这俩也加上useCallback就行了。。你看下警告的具体内容啊。。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题