Handle Asynchronous Events
You already know how to update the application state in response to user actions. Another common scenario is updating the state based on the response from a remote server. What makes this different is that communications like this are asynchronous, meaning the server request goes on in the background while your code keeps running. Nevertheless, setState also fits the bill when you need to update the state in response to a server. As an example, we’ll add save functionality to the word counter. This will show you how to use multiple states in the same application.
When the user clicks Save, the word counter sends the text they’ve typed to a server. When the server returns a successful response, we’ll display a success ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access