简体   繁体   English

如何解决“React Hook useEffect 缺少依赖项。包含它或删除依赖项数组”的问题?

[英]How to fix "React Hook useEffect has a missing dependency. Either include it or remove the dependency array" problem?

I want to use useEffect , but when I add getUpperGroup method, I get warning:我想使用useEffect ,但是当我添加getUpperGroup方法时,我收到警告:

React Hook useEffect has a missing dependency: 'getUpperGroups'. React Hook useEffect 缺少依赖项:“getUpperGroups”。 Either include it or remove the dependency array"包括它或删除依赖数组”

My code is:我的代码是:

useEffect(() => {
  getUpperGroups();
  setContentData(contentGroupData);
}, [contentGroupData]);


const [contentData, setContentData] = useState<Fundation[]>([] as Fundation[]);
const [upperGroups, setUpperGroups] = useState({});

const getUpperGroups = () => {

   let newUpperGroups = upperGroups;

   contentGroupData.forEach(content=>{
     newUpperGroups = {...newUpperGroups, [content.id]: content.title};
   })

   setUpperGroups(newUpperGroups);
}

You can either:您可以:

  1. Suppress that rule for the entire project: Go to .eslintrc file and change 'react-hooks/exhaustive-deps': 'error' to 'react-hooks/exhaustive-deps': 'warn' or 'react-hooks/exhaustive-deps': 'off'为整个项目禁止该规则:转到 .eslintrc 文件并将'react-hooks/exhaustive-deps': 'error'更改为'react-hooks/exhaustive-deps': 'warn''react-hooks/exhaustive-deps': 'off'

  2. Supress the rule only in this instance:仅在这种情况下禁止规则:

useEffect(() => {
    getUpperGroups();
    setContentData(contentGroupData);
}, [contentGroupData]);


const [contentData, setContentData] = useState<Fundation[]>([] as Fundation[]);
const [upperGroups, setUpperGroups] = useState({});

const getUpperGroups = () => {

    let newUpperGroups = upperGroups;

    contentGroupData.forEach(content=>{
        newUpperGroups = {...newUpperGroups, [content.id]: content.title};
    })

    setUpperGroups(newUpperGroups);
} // eslint-disable-line react-hooks/exhaustive-deps

You have two mistakes.你有两个错误。

1- You defined getUpperGroups after useEffect, so you can't add it to the list of useEffect dependencies. 1- 您在 useEffect 之后定义了 getUpperGroups,因此您无法将其添加到 useEffect 依赖项列表中。

2- if you add getUpperGroups to list of useEffect dependencies, useEffect will run on every re-render and you give a loop of re-render error. 2- 如果您将 getUpperGroups 添加到 useEffect 依赖项列表中,useEffect 将在每次重新渲染时运行,并且您会出现重新渲染错误的循环。

So there is two solutions.所以有两种解决方案。

1- Add getUpperGroups into useEffect 1- 将 getUpperGroups 添加到 useEffect

const [contentData, setContentData] = useState<Fundation[]>([] as Fundation[]);
const [upperGroups, setUpperGroups] = useState({});


useEffect(() => {
  
  const getUpperGroups = () => {

   let newUpperGroups = upperGroups;

   contentGroupData.forEach(content=>{
     newUpperGroups = {...newUpperGroups, [content.id]: content.title};
   })

   setUpperGroups(newUpperGroups);
  }

  getUpperGroups();
}, [contentGroupData]);

2- Disable eslint 2- 禁用 eslint

useEffect(() => {
  getUpperGroups();
  setContentData(contentGroupData);

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


const [contentData, setContentData] = useState<Fundation[]>([] as Fundation[]);
const [upperGroups, setUpperGroups] = useState({});

const getUpperGroups = () => {

   let newUpperGroups = upperGroups;

   contentGroupData.forEach(content=>{
     newUpperGroups = {...newUpperGroups, [content.id]: content.title};
   })

   setUpperGroups(newUpperGroups);
}

This one worked for me.这个对我有用。

Just add this comment at the last line of useEffect:只需在 useEffect 的最后一行添加此注释:

//eslint-disable-next-line //eslint-disable-next-line

useEffect(() => {
  getUpperGroups();
  setContentData(contentGroupData);
  //eslint-disable-next-line
}, [contentGroupData]);

Probably I would try to move the definition of getUpperGroups into useEffect .可能我会尝试将getUpperGroups的定义移动到useEffect Also you can use .map() and the previous state of your groups state.您也可以使用.map()groups状态的先前状态。

Try as the following:尝试如下:

useEffect(() => {
  const getUpperGroups = () => {
     setUpperGroups(prevUpperGroups => contentGroupData.map((content) => ({
        ...prevUpperGroups,
        [content.id]: content.title
     })))
  }

  getUpperGroups();
  setContentData(contentGroupData);
}, [contentGroupData]);

Just add getUpperGroups to deps array in useEffect只需添加getUpperGroups到DEPS阵列useEffect

useEffect(() => {
  getUpperGroups();
  setContentData(contentGroupData);
}, [contentGroupData, getUpperGroups]);

But better to move code itself to useEffect但最好将代码本身移动到useEffect

暂无
暂无

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

相关问题 如何修复“React Hook useEffect 缺少依赖项。 要么包含它,要么删除依赖数组”的问题? - How can I fix “React Hook useEffect has a missing dependency. Either include it or remove the dependency array” problem? React Hook useEffect 缺少依赖项。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:&#39;formData&#39;。 包括它或删除依赖项数组。 什么是依赖就是使用 - React Hook useEffect has a missing dependency: 'formData'. Either include it or remove the dependency array. what is dependency is use React Hook useCallback 缺少依赖项。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps - React Hook useCallback has a missing dependency. Either include it or remove the dependency array react-hooks/exhaustive-deps 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 缺少依赖项:&#39;context&#39;。 包括它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'context'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:&#39;fetchTracker&#39;。 包括它或删除依赖项数组 - 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM