Skip to the page

State: snapshots, updates and where state lives · mid

useReducer: explicit state transitions

Use useReducer when several values change together or the next state depends on the current one through clear rules; the reducer is a pure function you can unit-test, and dispatch is stable.

The notes

What you must know

  • const [state, dispatch] = useReducer(reducer, initialArg, init?); the reducer is (state, action) => newState and must be pure (no fetch, no random ids, no mutation).
  • Good fits: forms with related fields, wizards, a fetch status machine (idle, loading, success, error), undo/redo, anything where "events" describe what happened.
  • dispatch has a stable identity, so passing it down avoids creating new callbacks.
  • Reducers are easy to test without React: call them with a state and an action and check the result.
  • Async work does not go inside the reducer; do it in an event handler or effect and dispatch actions before and after.
  • useReducer + Context is a "mini Redux", but every consumer re-renders when the state object changes; there are no selectors.
  • It is not faster than useState; it is a code-organisation choice.

The red pen

Common traps: mistake → fix

  • Mistake: "useReducer is faster"

    Fix: same render cost; it is about clarity.

  • Mistake: "Put the API call in the reducer"

    Fix: reducers must be pure; React may call them more than once.

  • Mistake: "Context + useReducer replaces Redux for any app"

    Fix: no selectors, every consumer re-renders, no middleware or devtools.

Easy to hard

Questions interviewers ask, easy to hard

  1. When do you choose useReducer over useState?
  2. Refactor a form with six related useStates. What actions would you define?
  3. Is dispatch stable? Why does that matter?
  4. Where do async side effects go?
  5. When do you graduate from Context + useReducer to Zustand or Redux Toolkit?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview