useReducer
The React hook that manages state through a reducer function, returning the current state and a dispatch function.
In more detail
Instead of calling setters directly, components dispatch actions describing what happened, and the reducer decides how the state changes. It suits state where several values change together, such as a request that moves between idle, loading, success and error.
Example
const [state, dispatch] = useReducer(requestReducer, { status: "idle" });Related terms
- useState — The React hook that adds a state variable to a component and returns the current value with a function to change it.
- useOptimistic — The React hook that shows a temporary optimistic value while an Action is pending, then returns to the real state when it settles.
- reducer — A pure function that takes the current state and an action and returns the next state.
- dispatch function — The function returned by useReducer that sends an action to the reducer and schedules a re-render with the result.
- reducer action — A plain object describing something that happened, usually with a type string and optional payload data, sent to a reducer through dispatch.
- state machine — A model in which something is always in exactly one named state, such as idle, loading, success or error, and only defined events move it to another.
Taught in
- Server Components: Async Execution & Constraints — Next.js 15 & Modern App Router Architecture
- Optimistic UI: useOptimistic Rollbacks & Transitions — Next.js 15 & Modern App Router Architecture
- State with useState: Immutability And Asynchronous Batching — React 19 & Modern Component Architecture
- Complex State with useReducer: State Machines And Action Types — React 19 & Modern Component Architecture