简体   繁体   English

React Hook useEffect 缺少依赖项:'evaluate'。 包括它或删除依赖数组

[英]React Hook useEffect has a missing dependency: 'evaluate'. Either include it or remove the dependency array

How to add the code together in the useEffect?如何在useEffect中将代码一起添加? I cannot seem to find the solution.我似乎找不到解决方案。

const evaluate = () => {
     const [first, second] = openCards;
     enable();
     if (cards[first].type === cards[second].type) {
       setClearedCards((prev) => ({ ...prev, [cards[first].type]: true }));
       setOpenCards([]);
       return;
     }

To the useEffect:对使用效果:

useEffect(() => {
      console.log(openCards);
      let timeout = null;
      if (openCards.length === 2) {
        timeout = setTimeout(evaluate, 300);
      }
      return () => {
        clearTimeout(timeout);
      };
    }, [openCards]);

If you don't include it in the dependency array (second arg of useEffect ) it won't update when evaluate changes.如果您没有将它包含在依赖数组中( useEffect的第二个参数),它在评估更改时不会更新。

useEffect(() => {
      console.log(openCards);
      let timeout = null;
      if (openCards.length === 2) {
        timeout = setTimeout(evaluate, 300);
      }
      return () => {
        clearTimeout(timeout);
      };
    }, [openCards, evalute]); // Add it here

This is eslint warning, You can disable this这是 eslint 警告,您可以禁用它

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [openCards]);

暂无
暂无

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

相关问题 React Hook useEffect 缺少依赖项:'formData'。 包括它或删除依赖项数组。 什么是依赖就是使用 - React Hook useEffect has a missing dependency: 'formData'. Either include it or remove the dependency array. what is dependency is use React Hook useEffect 缺少一个依赖项:'handleLogout'。 要么包含它,要么移除依赖数组 react - React Hook useEffect has a missing dependency: 'handleLogout'. Either include it or remove the dependency array react React Hook React.useEffect 缺少依赖项:“loadData”。 包含它或删除依赖项数组 - React Hook React.useEffect has a missing dependency: 'loadData'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:'context'。 包括它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'context'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:'fetchTracker'。 包括它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'fetchTracker'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:'id'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'id'. Either include it or remove the dependency array React Hook useEffect 缺少一个依赖项:'tasks'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'tasks'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:'fetchComments'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'fetchComments'. Either include it or remove the dependency array 错误:React Hook useEffect 缺少依赖项:'dispatch'。 包括它或删除依赖数组 - ERROR: React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array React Hook useEffect 缺少一个依赖项:'refreshSells'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'refreshSells'. Either include it or remove the dependency array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM