Skip to content
Advertisement

Data fetched and set with setState inside useeffect doesnt appear in console log unless i set a timeout

JavaScript

So I am fetching data from my database for each item in the array groups and then I want to map through this data (twice because it’s an array inside an array) in the jsx part of the component. The problem though is that the state is changed “too late” This is my guess because the console log doesn’t show the data that is pushed into the gamesArray unless I put a timeout. The timeout doesn’t work though because then the jsx part doesn’t map through anything. The console.log(games) only shows the data if some code is changed in vs code after the site has rendered.

Any advice?

Advertisement

Answer

Try to use a custom hook to fetch all your games.
You can then use the data once they are all fetched:

JavaScript

Then retrieve the data using:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement