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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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 thatuseEffect
was 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). SincegetCourseByName
is an asynchronous operation, it will change the state after the page has loaded for the first time, thus triggering a re-render.