Reputation: 1608
I am trying to clean up the warnings in my DOM, and for every useEffect
where the deps are []
I get an error that says useEffect has a missing dependency. I want to trigger the effect when the component mounts, and I was under the impression that this was the way to do it. If thats the case, why the warning?
Here is the simple code im using
useEffect(() => {
setDispContext("NEW");
}, []);
Warning is React warning React Hook useEffect has a missing dependency: 'setDispContext'. Either include it or remove the dependency array react-hooks/exhaustive-deps
Upvotes: 3
Views: 96
Reputation: 393
Everything that you use unside useEffect must be inside the dependency array, so the right way would be:
useEffect(() => {
setDispContext("NEW");
}, [setDispContext]);
But sometimes you just need the useEffect to run once. If setDispContext won´t be change it can be put inside a useCallback. Otherwise the only waty would be to use :
useEffect(() => {
setDispContext("NEW");
}, []);// eslint-disable-line
So the eslint warning won´t show.
Upvotes: 2
Reputation: 317
Try:
useEffect(() => {
setDispContext("NEW");
// eslint-disable-line
}, []);
Upvotes: 0