useReducer
The problem. Several pieces of state that always change together. Loading, error and results are the standard example: every one of them has to be set correctly on every transition, and forgetting one leaves the interface in a state that should not exist, such as loading and error at the same time.
What a reducer does. It moves those transitions into one plain function that takes the current state and an action, and returns the next state. Every transition is written once, in one place, where you can read them side by side.
The reducer is an ordinary function, so it can be tested without rendering anything.
Mark this lesson complete
Signed in, your progress follows you to every device.