Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
When state pieces change together or the next state depends on several rules, a reducer centralizes the transitions, makes them testable and avoids inconsistent combinations.
useState is simplest for independent values. When a component has related fields, such as status, data and error for a request, or a form whose actions update several fields at once, separate setters can leave the state in impossible combinations. useReducer moves all transitions into one pure function that takes the current state and an action, so every change is explicit and easy to unit test. Dispatch is stable between renders, which helps when passing it down. A reducer is also a natural fit for modelling states as a discriminated union, such as idle, loading, success and error.