DEV Community

loading...

Discussion on: Fetching data with React Hooks

savagepixie profile image
SavagePixie Author

Thanks for such a detailed explanation! It is very helpful to understand what exactly you mean and to see what you've tried and why. Also, I'm glad you managed to get it to work as you wanted.

The reason why console.log() printed stuff twice is that useEffectwas running twice. Once when the page was first rendered (but before the data was fetched) and once on the re-render caused by retrieving the data. useEffect fires both on the first load of the page and on every state change (unless, obviously, you narrow it down in the dependency array). Since getCourseByName is an asynchronous operation, it will change the state after the page has loaded for the first time, thus triggering a re-render.