简体   繁体   English

react-hooks/exhaustive-deps 警告

[英]react-hooks/exhaustive-deps warning

I use react hook useEffect like the following code for fetching data and changing state in it.我像下面的代码一样使用 react hook useEffect来获取数据并在其中更改 state。 For avoiding the infinite loop I add an empty array as the second parameter of the useEffect and I get this warning.为了避免无限循环,我添加了一个空数组作为 useEffect 的第二个参数,我收到了这个警告。 Do I just ignore it or I have to fix it?我是忽略它还是必须修复它? If so, how can fix it?如果是这样,如何解决? I just want the componentDidMount effect here.我这里只想要 componentDidMount 效果。 I appreciate any idea?我很感激任何想法?

useEffect(() => {
 fetch('/login')
 .then(response => {
   if (response.ok) fetchAll() 
   else setLoading({ ...loading, signin: true, progress:false });     
 }).catch(() =>{
   setLoading({ ...loading, signin: true, progress:false });
 })
},[]);

try this, this should fix the warning试试这个,这应该可以解决警告

useEffect(() => {
 fetch('/login')
 .then(response => {
   if (response.ok) fetchAll() 
   else setLoading(loading => ({ ...loading, signin: true, progress:false }));     
 }).catch(() =>{
   setLoading(loading => ({ ...loading, signin: true, progress:false }));
 })
},[]);

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

相关问题 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps 带有自定义 IntersectionObserver 钩子的 react-hooks/exhaustive-deps 警告 - react-hooks/exhaustive-deps warning with custom IntersectionObserver hook useCallBack react-hooks/exhaustive-deps 警告 - useCallBack react-hooks/exhaustive-deps warning react-hooks/exhaustive-deps 警告还是无限循环? - react-hooks/exhaustive-deps warning or infinite loop? 解决 react-hooks/exhaustive-deps 警告的最佳方法 - Best way to resolve react-hooks/exhaustive-deps warning 设计React Hooks可以防止React-Hooks / Exhaustive-deps警告 - Designing React Hooks prevent react-hooks/exhaustive-deps warning react-hooks/exhaustive-deps 警告——不清楚行动方案 - react-hooks/exhaustive-deps warning--unclear about course of action 如何在全局范围内禁用 react-hooks/exhaustive-deps eslint 警告? - How to disable react-hooks/exhaustive-deps eslint warning globally? 是否有一个带有useEffect的无限滚动的实现,它不会触发react-hooks / exhaustive-deps lint警告? - Is there an implementation of infinite scroll with useEffect that doesn't trigger the react-hooks/exhaustive-deps lint warning? 从 useEffect 中改变多个状态会导致 react-hooks/exhaustive-deps 警告 - Mutate multiple states from within useEffect causes react-hooks/exhaustive-deps warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM