I had recently implemented something similar to this.
const[loading,setLoading]=useState(false)
I did the same thing, initializing loading state to false. However, my coworkers argued that it could be
const[loading,setLoading]=useState(true)
because the Hook will eventually set loading state to true any way. I argued that the Hook isn't actually fetching until the effect runs, which is why I initialized loading state to false. I eventually gave in and initialized loading state to true because I felt like we were bikeshedding.
Eventually found one good thing about initializing loading state to true. If the component using this custom fetch Hook rendered some other content, it won't flicker. In your case, this shouldn't happen though since BookList isn't rendering anything other than the fetched books.
This causes annoying flashing, before promise is resolved. Is there a way to show this loader inside actual markup, instead of this single element with Loading...?
I had recently implemented something similar to this.
I did the same thing, initializing
loadingstate tofalse. However, my coworkers argued that it could bebecause the Hook will eventually set
loadingstate totrueany way. I argued that the Hook isn't actually fetching until the effect runs, which is why I initializedloadingstate tofalse. I eventually gave in and initializedloadingstate totruebecause I felt like we were bikeshedding.Anyway, what do you think?
Eventually found one good thing about initializing
loadingstate totrue. If the component using this custom fetch Hook rendered some other content, it won't flicker. In your case, this shouldn't happen though since BookList isn't rendering anything other than the fetched books.That's true! I'll update the post and add your note to it.
How do you deal with this part:
This causes annoying flashing, before promise is resolved. Is there a way to show this loader inside actual markup, instead of this single element with Loading...?
Loved your explanation. Can you describe useEffect in detail in next post. I couldn't understand the use case
Hey, thank you a lot.
I won't write about useEffect in the next post but I just wrote a quick explanation here:
dev.to/bdbch/a-quick-explanation-o...
Let me know if it helped you! :)
Nice Article...
useful info, remarkable clarity, one small typo (closing paren):
Thank you very much! Fixed!
I'd say because of the comments which are cluttering the actual source code and the new way to update states etc.
This code is subject to race conditions. Check my post about that subject here: dev.to/sebastienlorber/handling-ap...
what would be the pros and cons on using fetch vs axios ?