简体   繁体   English

React Hook useEffect 缺少依赖项:'setValid'。 如何删除此警告

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

相关问题 警告:React Hook useEffect 缺少依赖项 - warning: React Hook useEffect has a missing dependency React useEffect 钩子缺少依赖项警告 - React useEffect hook has a missing dependency warning React Hook useEffect 缺少依赖项警告 - React Hook useEffect has a missing dependency warning 如何修复这个“React Hook useEffect has a missing dependency”警告? - How to fix this “React Hook useEffect has a missing dependency” warning? 我应该如何删除“ React Hook useEffect缺少依赖项”警告? - How should i remove warning “React Hook useEffect has a missing dependency”? 如何解决此警告:“React Hook useEffect 缺少依赖项:'history'”? - How to fix this warning: “React Hook useEffect has a missing dependency: 'history'”? 如何删除“React Hook useEffect has missing dependencies”警告 - How to remove the “React Hook useEffect has missing dependencies” warning 当 deps 为 [] 时,React 警告 React Hook useEffect 缺少依赖项 - React warning React Hook useEffect has a missing dependency when the deps are [] 'React Hook useEffect has an missing dependency' 警告功能 - 'React Hook useEffect has a missing dependency' warning with function 我的应用程序的“React Hook useEffect 缺少依赖项”警告 - My App's "React Hook useEffect has a missing dependency" warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM