Lesson 25 of 30
83%
useReducer
useReducer is useState for when the next value depends on a few action types.
You send an action. A reducer function returns the next state.
A tiny counter
javascript
Reducers must be pure
Same state and action in, same next state out. No fetch, no random, no mutate.
Return a new object.
When to pick it
1
Many events update the same object
2
The next state needs the previous state in a careful way
3
You want to test the logic without rendering
A single boolean still belongs in useState.
Tip: Name action types like events: inc, add_item, submit. Not like setter names.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.