React Hook useEffect has a missing dependency: 'setValid' . How to remove this warning

Viewed 39

Im new to react and have been using useEffect for the first time. 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'.

2 Answers

easy just pass setValid as the dependency

   setValid(true);
}, [setValid]};

As NeERAJ TK mentioned, React linter is complaining about not having setValid in the array of dependencies:

useEffect(() => {
   setValid(true);
}, [setValid]};

I assume that setValid is a setter generated by the use hook inside your component. 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.

In this case, I would suggest not using useEffect in general, and pass a default value to setState:

const [valid, setValid] = useEffect(true);
Related