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 ...
Get React for Real now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.