[英]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.