useReducer vs useState
Question
When should you prefer useReducer over useState?
Hint
useReducer centralizes state transitions with a reducer function.
Explanation
useReducer is preferable when state has multiple sub-values, transitions depend on previous state, or the next state depends on complex logic. It centralizes update logic in a reducer function: useReducer(reducer, initialState). Dispatch actions: dispatch({type: "increment"}). This makes state changes predictable and testable. For simple state (a single value), useState is simpler. useReducer also helps avoid passing callbacks down — pass dispatch instead.
Related Resources
Related Lessons
- useReducer
Learn useReducer.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.