Skip to content
Advertisement

How to properly execute logic right after setState updates the react functional component state?

Currently, I am making a profile page where it will initially fetch the user profile details from an API endpoint. I want to have a loading indicator representing the fetch status. I am now using useState hook where I store a boolean value for isLoading state. However, after reading the documentation about useState it stated that it may be asynchronous. Hence, how do I properly update isLoading to true first then execute the fetch logic? Right now here is my code snippet.

JavaScript

Advertisement

Answer

In your example use case you can simply set isLoading to true initially and run your fetch on-mount.

JavaScript

but if you want to watch for changes to isLoading say for a reload button, you can set it false initially, set it to true in an on-mount useEffect and have your fetch useEffect dependent on isLoading

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