简体   繁体   English

使用 n useEffect 了解详尽的 deps ESlint 警告

[英]Understanding Exhaustive deps ESlint warning with n useEffect

Cant seem to get my head around the right approach for this.似乎无法理解正确的方法。 Want to get ride of the warning and not by having the inline ignore warning comment.想要摆脱警告,而不是通过内联忽略警告评论。 Probably useCallback is in the direction of the solution.可能 useCallback 是解决方案的方向。 I just want to check only once when functional component gets initialised wether if the Store needs, conditionally.如果商店需要,我只想检查一次功能组件是否有条件地初始化。 Thats all.就这样。

Line 72:6: React Hook useEffect has missing dependencies: 'dispatch' and 'storedSlugItems'. Either include them or remove the dependency array react-hooks/exhaustive-deps

useEffect(() => {
  if (storedSlugItems.length > 0) {
    dispatch(setSlugItems(storedSlugItems));
  }
  // eslint-disable-next-line 
}, []);

Just add dispatch to useEffect dependencies:只需将dispatch添加到useEffect依赖项:

useEffect(() => {
  if (storedSlugItems.length > 0) {
    dispatch(setSlugItems(storedSlugItems));
  }
}, [dispatch]);

It probably also matters what dispatch is and where it comes from, but adding it to the dependencies should work. dispatch是什么以及它来自哪里可能也很重要,但是将它添加到依赖项应该可以工作。

If it is Redux dispatch - the dispatch function reference will be stable as long as the same store instance is being passed to the.如果是Redux 调度- 只要将相同的存储实例传递给,调度 function 引用将是稳定的。 Normally, that store instance never changes in an application.通常,该存储实例永远不会在应用程序中更改。

However, the React hooks lint rules do not know that dispatch should be stable, and will warn that the dispatch variable should be added to dependency arrays for useEffect and useCallback.但是,React hooks lint 规则不知道 dispatch 应该是稳定的,并且会警告应该将 dispatch 变量添加到依赖 arrays 以用于 useEffect 和 useCallback。 The simplest solution is to do just that - to add it to the dependencies:)最简单的解决方案就是这样做 - 将其添加到依赖项中:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 useEffect 中的 Eslint 详尽 deps 警告 - Eslint exhaustive deps warning in useEffect Eslint React Hooks错误:eslint-plugin-react-hooks用尽详尽的警告警告useEffect中的功能依赖项 - Eslint React Hooks Error: eslint-plugin-react-hooks exhaustive deps warning for a function dependency in useEffect useEffect 和 ESlint 穷举-deps 规则 - useEffect and ESlint exhaustive-deps rule 使用 eslint Exclusive-deps 响应订阅/取消订阅的 useEffect 依赖项 - React useEffect dependencies for subscribe/unsubscribe with eslint exhaustive-deps useEffect 依赖数组和 ESLint Exclusive-deps 规则 - useEffect dependency array and ESLint exhaustive-deps rule useEffect 具有详尽的 deps 循环 - useEffect with exhaustive deps loop 了解 react-hooks/exhaustive-deps useEffect 和调度事件 - understanding react-hooks/exhaustive-deps useEffect and dispatching events 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps useEffect exhaustive-deps 警告:这试图避免哪些可能的问题? - useEffect exhaustive-deps warning: what possible issues is this trying to avoid? React.useEffect:尽管来自 ESLint(react-hooks/exhaustive-deps) 的警告,组件仍使用依赖项数组中的 window.location.pathname 重新渲染 - React.useEffect: Component re-renders with window.location.pathname in dependency array despite warning from ESLint(react-hooks/exhaustive-deps)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM