I receive data back OK from the server with Axios, but when I try to set the state with the response data, the state won't update. The state is stored in context provider file, but I've tried state in the function itself with no luck either.
It is not a problem with the state itself, or the context provider as it works for other things.
What am I doing wrong? If anyone can advise please? Any help would be gratefully appreciated/acknowledged. Thanks.
const CheckUser = async () => {
// state from the context file
const { validUser, setValidUser } = useContext(UserContext);
useEffect(() => {
axios
.post(
`${serverURL}/checkuser`,
{ someData: 1 },
{
withCredentials: true,
credentials: 'include',
}
)
.then((response) => {
if (response.data) {
// this will log the result fine:
console.log(response.data.validUser);
// but the state won't update with it:
setValidUser(response.data.validUser);
} else {
console.log('Failed to get data');
}
});
}, []);
Loading
}`. – cbr Oct 11 '20 at 19:02