0
Challenge · 31Hooks & Effects

useReducer

Coreused in ~45% of apps

When a component grows a tangle of related state and a dozen setters that all touch it, the update logic gets smeared across your handlers and impossible to follow. useReducer pulls all of that logic into one pure function you can read, test, and reason about in isolation.

Core idea. useReducer moves your state-update logic out of the component into a single pure function — a reducer (state, action) => newState. Components dispatch ACTIONS describing what happened; the reducer decides the resulting next state, immutably.
Playground, this isn't a screenshot
your event handlers dispatch an action
action
dispatch one →
reducer(state, action)
switch (action.type) {
case "increment":
case "decrement":
case "reset":
}
new state (out)
{ count: 0 }NEW OBJ
action timeline — state is the reduce() of these

No actions yet. Dispatch one above and watch it land here.

Handlers say what happened (the action); the pure reducer decides the next state and always returns a brand-new object — the old one is never mutated.

↑ play a little first, then dig in