Quick question, how do I fix the warning that I get when I try to use useEffect in my app. Here is my code:
const userUrl = `${process.env.REACT_APP_API_URL}/${id}`;
const [newUser, setNewUser] = useState({
name: "",
email: "",
status: "",
gender: "",
});
useEffect(() => {
if (id) {
const getUser = async () => {
try {
const response = await axios.get(userUrl);
const apiData = response.data.data;
setNewUser({
...newUser,
name: apiData.name,
email: apiData.email,
status: apiData.status,
gender: apiData.gender,
});
} catch (err) {
console.error(err);
}
};
getUser();
} else {
setNewUser({
...newUser,
status: "active",
gender: "male",
});
}
}, [userUrl, id]);
This is the full warning message: React Hook useEffect has a missing dependency: 'newUser'. Either include it or remove the dependency array. You can also do a functional update 'setNewUser(n => ...)' if you only need 'newUser' in the 'setNewUser' call
I tried adding the newUser inside the second parameter of the useEffect hook, but then I get the maximum depth error, so if anyone know how to fix this, I would gladly appreciate that.