Variable is not updating data React

Viewed 460

I have in my App.jsx a fetch to the backend to request the user data and then in a child component I use that info to show or not something (depending if we have user or not). With the React Dev Tools extension for Chrome, I can see that the variable has information, but I don't understand why my code is not working.

const [myEvents, setMyEvents] = useState(null)

const eventsFetch = async () => {

    // I use this conditional to wait until the backend response.
    if(user == null) {
        console.log('test')
        setTimeout(eventsFetch, 250)
    } else {
        const resEvents = await getAllEventsUser(user._id);
        setMyEvents(resEvents);
        
    }
}

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

The conditional is always going where the console.log is.

1 Answers

I had to add the variable user to the useEffect dependecy and now it's working

Related