简体   繁体   English

React钩子使用eslint-plugin-react-hooks @ next附加不需要的值

[英]React hooks appends unwanted value with eslint-plugin-react-hooks@next

I have a useEffect hook in my component which call a Redux action to fetch some data. 我的组件中有一个useEffect钩子,它调用Redux动作来获取一些数据。

useEffect(
 () => {
   props.getClientSummaryAction();
  },[]
);

When I go to save the file the linter does this. 当我去保存文件时,linter会这样做。

useEffect(
  () => {
    props.getClientSummaryAction();
 }, [props] 
);

Which obviously send my component into an infinite loop as getClientSummaryAction fetches some data which updates the props. 这显然将我的组件发送到无限循环,因为getClientSummaryAction获取一些更新道具的数据。

I have used deconstruction like this and the linter updates the array. 我使用了这样的解构,而linter更新了数组。

  const { getClientSummaryAction } = props;

  useEffect(
    () => {
      getClientSummaryAction();
    },
    [getClientSummaryAction]
  );

Which I am fine with but it doesn't really make sense because getClientSummaryAction will obviously never be updated, its a function. 我很好,但它没有用,因为getClientSummaryAction显然永远不会更新,它是一个功能。

I just want to know why the linter is doing this and whether this is best practice. 我只是想知道为什么linter这样做以及这是否是最好的做法。

It's not unwanted. 这不是不需要的。 You know for a fact that the dependency is not going to change, but React can possibly know that. 你知道依赖不会改变的事实,但React可能知道这一点。 The rule is pretty clear: 规则很明确:

Either pass an array containing all dependencies or don't pass anything to the second argument and let React keep track of changes. 传递一个包含所有依赖项的数组,或者不传递任何东西到第二个参数,让React跟踪更改。

Pass the second argument to useEffect is to tell React that you are in charge of hook's call. 将第二个参数传递给useEffect是告诉React你负责hook的调用。 So when you don't pass a dependency that is included inside your effect eslint will see this as a possible memory leak and will warn you. 因此,当您没有传递效果中包含的依赖项时, eslint会将此视为可能的内存泄漏,并会警告您。 DO NOT disable the rule just continue to pass the dependency as you are already doing. 不要禁用规则只是继续传递依赖关系,就像你已经在做的那样。 May feel redundant but it's for the best. 可能感到多余,但它是最好的。

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

相关问题 带有依赖列表和 eslint-plugin-react-hooks 的自定义钩子 - Custom hooks with dependency lists and eslint-plugin-react-hooks React,ESLint:eslint-plugin-react-hooks 依赖于 object 中的 function - React, ESLint: eslint-plugin-react-hooks dependency on a function in an object 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 在打字稿中启用react-hooks eslint插件。 tslint中没有插件 - enabling the react-hooks eslint plugin in typescript. no plugins in tslint 为什么 eslint-plugin-react-hooks 在有条件地使用 React 钩子时不会发出警告? - Why eslint-plugin-react-hooks doesn't warn when using react hooks conditionally? eslint-plugin-react-hooks 出现意外错误(react-hooks/exhaustive-deps) - eslint-plugin-react-hooks is giving unexpected errors (react-hooks/exhaustive-deps) React,ESLint:eslint-plugin-react-hooks 显示不正确的“缺少依赖项” - React, ESLint: eslint-plugin-react-hooks shows incorrect “missing dependency” 使用 Hooks 响应 InputForm 值 - React InputForm Value With Hooks 在 React 中使用回调挂钩的 Eslint 错误 - Eslint error on using callback hooks in React Eslint 反应钩子/详尽的 derps 递归 - Eslint react-hooks/exhaustive derps recursion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM