关于 reactjs:useEffect() React Hook Dependency Array lint 规则 | 珊瑚贝

useEffect() React Hook Dependency Array lint rule


lint 规则强制我将 \\’dispatch\\’ 也作为 useEffect() 的依赖项。

(如果我不将调度添加为依赖项,它会引发警告”React Hook useEffect 缺少依赖项:\\’dispatch\\’。要么包含它,要么删除依赖项数组。”)

是否可以将 dispatch 或任何其他函数列为依赖项?有没有更好的方法在不禁用 lint 规则的情况下使用?

1
2
3
4
5
useEffect(() => {
    if (debouncedSearchText) {
      dispatch(getUsers(pageIndex, rows, debouncedSearchText));
    }
},[debouncedSearchText, pageIndex, dispatch]);

可以将函数添加为依赖项,但请记住,组件层次结构中的函数应该package在 useCallback 钩子中,因为在重新渲染时它不会更改引用,除非给出依赖项在 useCallback 中发生了变化。库编写者已经公开了记忆函数,因此无需将这些函数package在 useCallback 中。

  • 注意,dispatch 已经很稳定了,所以可以包含在依赖数组中


来源:https://www.codenong.com/61939253/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9418.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?