DEV Community

Discussion on: React Hooks for Data Part 1 - Fetching Data

Collapse
 
2ezpz2plzme profile image
Steven Liao

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.

Anyway, what do you think?

Collapse
 
2ezpz2plzme profile image
Steven Liao

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.

Collapse
 
bdbch profile image
bdbch

That's true! I'll update the post and add your note to it.