Predict Reducer State
Predict Output Medium
Question
What is the final state after these dispatches?
Code
function reducer(state, action) {
switch (action.type) {
case "add": return { count: state.count + action.payload };
case "reset": return { count: 0 };
default: return state;
}
}
const [state, dispatch] = useReducer(reducer, { count: 0 });
dispatch({ type: "add", payload: 5 });
dispatch({ type: "add", payload: 3 });
dispatch({ type: "reset" });
// What is state.count?
Hint
Process each action in order.
Explanation
Starting at count 0: 'add 5' → 5, 'add 3' → 8, 'reset' → 0. The final count is 0.
Related Resources
Related Lessons
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.