简体   繁体   English

反应钩子中无效的详尽部门

[英]Invalid exhaustive deps in react hooks

I am using a react hook to fetch the data from covid api using axios but it throws an error of我正在使用反应挂钩使用 axios 从 covid api 获取数据,但它会引发错误

React Hook useEffect has a missing dependency: 'countries'. Either include it or remove the dependency array  react-hooks/exhaustive-deps

where countries is my state我的 state 在哪里

Code:代码:

const [countries,setCountries]=useState([]);

  useEffect(()=>{
    axios.get('https://disease.sh/v3/covid-19/countries').then(response=>{
      //console.log(response.data);
      setCountries(response.data);
      console.log(countries);
    }).catch((error)=>{
      console.log(error);
    })

  },[]);

I have tried doing every solutions provided by other forums but this is prefectly fine mentioned by some solutions.我已经尝试过其他论坛提供的所有解决方案,但是某些解决方案提到了这一点。 Why isnt this code working?为什么这段代码不起作用?

If you use countries in your hook, you should add it to your deps.如果您在挂钩中使用countries地区,则应将其添加到您的部门。 Otherwise you will read a stale value because the state value countries was not yet updated:否则,您将读取过时的值,因为 state 值countries尚未更新:

useEffect(()=>{
    axios.get('https://disease.sh/v3/covid-19/countries').then(response=>{
      //console.log(response.data);
      setCountries(response.data);
      console.log(countries);
    }).catch((error)=>{
      console.log(error);
    })
  },[countries]);

However in your case I would advice against adding countries to your deps , because you also call setCountries in your hook.但是,在您的情况下,我建议您不要将countries地区添加到您的 deps中,因为您还会在钩子中调用setCountries This way your hook would be entered again because countries has changed.这样你的钩子就会再次进入,因为countries已经改变了。 You should rather log the value of countries outside of your hook if you need it.如果需要,您应该记录钩子之外的国家/地区的价值。

console.log(countries);

useEffect(()=>{
    axios.get('https://disease.sh/v3/covid-19/countries').then(response=>{
      //console.log(response.data);
      setCountries(response.data);
    }).catch((error)=>{
      console.log(error);
    })
  },[]);

If you are adding any variables which can be changed in useEffect it will ask you to update the dependency array.You are logging countries that's why you are seeing this warning.如果您要添加任何可以在 useEffect 中更改的变量,它会要求您更新依赖数组。您正在记录国家,这就是您看到此警告的原因。

If you want to understand useEffect in depth, and about the dependency array blog by Dan Abramov about A complete Guide To Useeffect will be very helpful如果您想深入了解 useEffect 以及 Dan Abramov 的依赖数组博客,关于A complete Guide To Useeffect将非常有帮助

暂无
暂无

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

相关问题 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps 未找到规则“re​​act-hooks/exhaustive-deps”的定义 - Definition for rule 'react-hooks/exhaustive-deps' was not found React hooks - useEffect exhaustive deps - location.hash 的循环依赖 - React hooks - useEffect exhaustive deps - cyclic dependency on location.hash useEffect 和 'react-hooks/exhaustive-deps' linting - useEffect and 'react-hooks/exhaustive-deps' linting 带有自定义 IntersectionObserver 钩子的 react-hooks/exhaustive-deps 警告 - react-hooks/exhaustive-deps warning with custom IntersectionObserver hook 设计React Hooks可以防止React-Hooks / Exhaustive-deps警告 - Designing React Hooks prevent react-hooks/exhaustive-deps warning React Hooks react-hooks/exhaustive-deps eslint 规则似乎过于敏感 - React Hooks react-hooks/exhaustive-deps eslint rule seems overly sensitive 如何在React中使用钩子实现componentDidMount以使其符合EsLint规则“ react-hooks / exhaustive-deps”:“ warn”? - How to implement componentDidMount with hooks in React to be in line with the EsLint rule “react-hooks/exhaustive-deps”: “warn”? 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挂钩:如何使用react-hooks / exhaustive-deps规则在没有无限循环的情况下读取和更新挂钩中的状态 - React hooks: How to read & update state in hooks without infinite loops with react-hooks/exhaustive-deps rule
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM