useState is for simple state management, while useReducer is for more complex state logic, often involving multiple sub-values or next state depending on the previous one.
01
01
Understand the problem
hooksstate managementuseStateuseReducer
02
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Stuck? AI Nudge Available
Get a conceptual hint to guide your logic without spoiling the final implementation.
03
03
Study the solution
The solution is waiting
Give it an honest attempt first — then compare your thinking with the full walkthrough.
04
04
Read the code
A counter with useReducer
import { useReducer } from "react";
function reducer(state, action) {
switch (action.type) {
case "inc": return { count: state.count + 1 };
case "dec": return { count: state.count - 1 };
case "reset": return { count: 0 };
default: throw new Error("unknown action");
}
}
export default function App() {
const [state, dispatch] = useReducer(reducer, { count: 0 });
return (
<div style={{ padding: 24, fontFamily: "system-ui" }}>
<h3>Count: {state.count}</h3>
<button onClick={() => dispatch({ type: "dec" })}>-</button>
<button onClick={() => dispatch({ type: "inc" })}>+</button>
<button onClick={() => dispatch({ type: "reset" })}>reset</button>
</div>
);
}05
05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.