The useReducer hook lets a React component manage state through a reducer function: instead of setting values directly, you dispatch actions that describe what happened, and the reducer returns the next state. You call it as const [state, dispatch] = useReducer(reducer, initialState). It is the usual choice when one piece of state has several related fields or many different ways of changing.
This guide builds a small shopping-cart example with the usereducer hook, covering the reducer, actions, the initial state and a few common mistakes. The examples target React 19 with function components.
useReducer takes two required arguments and returns an array of two items:
(state, action) => newState.It returns the current state and a dispatch function. Calling dispatch(action) schedules a re-render: React runs your reducer with the current state and the action, and uses the returned value as the next state.
The reducer must be a pure function. It must not call APIs, read the clock, generate random numbers, mutate its arguments or touch anything outside itself. In development with Strict Mode, React calls the reducer twice on purpose to help you spot impurities.
The smallest working example is a counter. The reducer switches on action.type:
The component never says how the count changes. It only reports what the user did. All the rules live in reducer, which sits outside the component and can be tested on its own.
An action is a plain object. By convention it has a type string, and it may carry extra data, often called payload. Name the type after what happened ("item_added"), not after the state change you want ("set_items"). That keeps the reducer in charge of the result.
Dispatching from a component then reads clearly:
A reducer must return a new object rather than changing the old one. React compares the previous and next state with Object.is, so mutating state.items.push(...) and returning the same object means React may skip the re-render, and Strict Mode will show doubled changes.
Use the spread operator, map, filter and slice to build new values, as in the cart reducer above. For deeply nested state, many teams use the Immer library through useImmerReducer from the use-immer package, which lets you write mutating-looking code that produces immutable updates.
If building the initial state is expensive, pass a third argument, an init function. React calls init(initialArg) once and uses the result as the starting state:
Without init, an expression such as createInitialState() as the second argument runs on every render, even though React ignores its result after the first one.
undefined.fetch calls in the reducer. Run them in an event handler or an effect, then dispatch the result.dispatch during render, which causes a loop of re-renders.state.items during render instead.No. The idea of reducers and actions is similar, but useReducer is a built-in hook that manages state for one component tree branch. Redux is a separate library with a global store, middleware and developer tooling.
No. React guarantees that the dispatch function has a stable identity, so you can omit it from dependency arrays and pass it to memoized children without causing extra renders.
It can, but throwing an error for an unknown action type is safer because it exposes typos immediately. Returning state unchanged is also acceptable if you want to ignore unknown actions.
No. The state variable in the current render does not change. The new value appears in the next render that React schedules after the dispatch.
useReducer stores state and updates it by running a pure reducer on dispatched actions. Keep the reducer free of side effects, return new objects instead of mutating, describe actions by what happened, and compute derived values during render. Use the lazy init argument when the starting state is costly to build.