I have a certain query string I am looking for and when that gets passed on page loading I need to update my user's status to "premium". I have:
useEffect(() => {
const queryString = require('query-string');
const values = queryString.parse(window.location.search);
console.log('before' + user.premium);
if (values.premium !== ''){
setUser({...user, premium: true});
}
console.log('after' + user.premium);
}, []);
I am using React functional components and my user is an object that contains many other values, in addition to "premium" key. From what I understand, passing the empty array []
will cause useEffect to run only on page load. Doing so, however, I get an error:
React Hook useEffect has a missing dependency: 'user'.
. BUT, when I include user
in that array, the page continually reloads (since I am updating the value of it). I've also tried passing in setUser
and that didn't work.