QuickDevGuide

Home › React

useReducer for Complex State — React

useReducer(reducer, initialState) centralises state transitions in a pure function (state, action) => newState. Prefer it over several useState calls when: the next state depends on the previous one in non-trivial ways, many event handlers update the same data, you want the logic testable without rendering anything. Anatomy Reducers must be pure: no API calls, no mutations, no randomness — return a new object instead. Name actions after what happened ("added", "toggled") rather than what to set.

Key Takeaways