[英]React Hook useEffect has a missing dependency: 'setValid' . How to remove this warning
Im new to react and have been using useEffect for the first time.我是新手,第一次使用 useEffect。 Im getting warning of missing dependecy, but the app works perfectly.
我收到缺少依赖项的警告,但该应用程序运行良好。 How can I solve the warning?
我该如何解决警告? Or is it okay to ignore this warning?
还是可以忽略此警告?
useEffect(() => {
setValid(true);
}, []};
Im getting the warning: React Hook useEffect has a missing dependency: 'setValid'.我收到警告:React Hook useEffect 缺少依赖项:'setValid'。
easy just pass setValid
as the dependency很容易,只需将
setValid
作为依赖项传递
setValid(true);
}, [setValid]};
As NeERAJ TK mentioned, React linter is complaining about not having setValid in the array of dependencies:正如 NeERAJ TK 提到的,React linter 抱怨在依赖数组中没有 setValid :
useEffect(() => {
setValid(true);
}, [setValid]};
I assume that setValid
is a setter generated by the use hook inside your component.我假设
setValid
是由组件内的 use 钩子生成的设置器。 If you will pass a function in the array of dependencies and will just call a setState
inside your useEffect
, you will run into an infinite loop so you will have to memoize or find a different approach.如果您将在依赖项数组中传递 function 并且只在您的
useEffect
中调用setState
,您将陷入无限循环,因此您将不得不记忆或寻找不同的方法。
In this case, I would suggest not using useEffect
in general, and pass a default value to setState
:在这种情况下,我建议一般不要使用
useEffect
,并将默认值传递给setState
:
const [valid, setValid] = useEffect(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.