The useReducer hook is an alternative to useState for managing complex state logic. It is especially useful when state updates depend on previous state or involve multiple related values.
useReducer?Use useReducer when:
State logic is complex
Multiple state values are related
Many different actions update the state
You want predictable state transitions
It follows a pattern similar to Redux, but is built into React.
state → current state
dispatch → function to trigger actions
reducer → function that updates state
initialState → starting state
A reducer takes the current state and an action, then returns the new state.
Actions are plain objects:
With payload:
Reducer:
Useful for expensive initial state calculations
useReducer vs useStateuseReducer | useState |
|---|---|
| Complex logic | Simple state |
| Centralized updates | Direct updates |
| Predictable transitions | Quick and simple |
| Redux-like | Minimal |
Forms with multiple fields
Complex UI state
State transitions with rules
Managing global state with Context
useReducer with useContext
Keep reducers pure
Do not mutate state
Use clear action types
Split large reducers
useReducer manages complex state
Reducers define how state changes
Actions describe what happened
Ideal for predictable state logic
Take quizzes related to this topic and see where you stand!
Start Quiz Now