5 State management and the application’s lifecycle
This chapter covers
- What state management is
- Implementing a state management solution
- Mapping JavaScript events to commands that change the state
- Updating the state using reducer functions
- Re-rendering the view when the state changes
I remember some time ago I went to the coast in the south of Spain, to a small village in Cadiz. There was this restaurant, so popular that they’d run out of food quickly; they had a limited number of each of the dishes they served, and they would update a chalkboard with the number of servings they had left. When there was no more of a particular dish, they’d cross it out. The waiter took orders from the customers and updated the chalkboard to reflect the new state of the restaurant: having one less of that dish. Us, the customers, could easily know what we could order by looking at the chalkboard. But from time to time, and due to the work load, the waiter forgot to update the chalkboard, and customers would find out the dish they’ve been waiting so long in line to order was sold out. You can picture the drama. It’s clearly important for the restaurant to have an updated chalkboard that matched the remaining servings of each dish.