What is useReducer in React?
Answer
useReducer is a React hook for managing complex state logic locally — it is Redux-like state management scoped to a single component or subtree. Signature: const [state, dispatch] = useReducer(reducer, initialState);. Define a reducer: function reducer(state, action) { switch (action.type) { case "increment": return { count: state.count + 1 }; case "reset": return initialState; default: return state; } }. Dispatch: dispatch({ type: "increment" }). vs useState: use useReducer when state update logic is complex (multiple sub-values, next state depends on previous), when you need to pass the dispatch function down (stable reference), or when multiple state updates should happen together atomically. vs Redux: useReducer is scoped to the component tree below — not global. For global state shared across many components, use Redux or another library. Combine useReducer + Context to build a lightweight Redux-like system without any library.
Previous
What is the Context API for state management?
Next
How do you handle async operations in Redux Toolkit with createAsyncThunk?