简体   繁体   English

ESLint:收到错误“React Hook useEffect has missing dependencies”

[英]ESLint: Getting the error “React Hook useEffect has missing dependencies”

I am currently making a TodoList app using Recoil and React and I am getting the error:我目前正在使用 Recoil 和 React 制作 TodoList 应用程序,但出现错误:

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

I believe this is preventing the app from building.我相信这会阻止应用程序构建。 It works in development mode.它在开发模式下工作。

Please help me fix this!请帮我解决这个问题!

Here is the concerned file:以下是相关文件:

export default function Todos() {
  // Global State Hooks
  const [todoList, setTodoList] = useRecoilState(todoState);
  const changing = useRevoilValue(changeState);
  const show = useRecoilValue(addState);
  const removing = useRecoilValue(removeState);
  const priority = useRecoilValue(priorityState);

  // **** CODE SEGMENT STARTS HERE ****

  useEffect(() => {
    if (priority) {
      setTodoList(
        todoList.slice().sort(function (a, b) {
          return a.priority - b.priority; // By priority
        })
      );
    } else {
      setTodoList(
        todoList.slice().sort(function (a, b) {
          return a.title.localeCompare(b.title); // By name
        })
      );
    }
    // Watches priority for sortButton, and editing, adding and deleting modes etc.
  }, [priority, changing, show, removing]);

  // **** CODE SEGMENT ENDS HERE ****

  return (
    <div className={styles.todos}>
      {/* Dynamically render  todo items */}
      {todoList.map((todo, index) => (
        <span key={todo.id} className={styles.todoItem}>
          <TodoItem key={index} item={todo} />
        </span>
      ))}
      {/* Todo Form only appears in adding mode */}
      {show ? <TodoForm /> : null}
    </div>
  );
}

This is a warning that React gives back.这是 React 回馈的警告。 By this React wants us to use the setTodoList and todoList are the dependencies in the array of the 2nd argument.通过这个 React 希望我们使用setTodoListtodoList是第二个参数数组中的依赖项。

  useEffect(() => {
    if (priority) {
      setTodoList(
        todoList.slice().sort(function (a, b) {
          return a.priority - b.priority; // By priority
        })
      );
    } else {
      setTodoList(
        todoList.slice().sort(function (a, b) {
          return a.title.localeCompare(b.title); // By name
        })
      );
    }
    // Watches priority for sortButton, and editing, adding and deleting modes etc.
  }, [priority, changing, show, removing,todoList,setTodoList]); // Added todoList and setTodoList as dependencies

However we have to be cautious of not creating an infinite loop in our Component.但是,我们必须小心不要在我们的组件中创建infinite loop Because setTodoList() will trigger a re-render of the component and again useEffect() will be invoked.因为setTodoList()将触发组件的重新渲染,并且将再次调用useEffect() This will keep happening.这将继续发生。

And if this happens The solution is to use useCallback() around setTodoList to prevent re-creation of the function every time (We may need to format our code a bit as well)如果发生这种情况解决方案是在 setTodoList 周围使用setTodoList useCallback()来防止每次重新创建 function (我们可能还需要稍微格式化我们的代码)

This error is caused by your eslint rules: https://reactjs.org/docs/hooks-rules.html You can turn off the rule in your eslint.rc by adding:此错误是由您的 eslint 规则引起的: https://reactjs.org/docs/hooks-rules.html您可以通过添加以下内容来关闭eslint.rc中的规则:

{
  "rules": {
    "react-hooks/exhaustive-deps": "off"
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM