Skip to content

QuizNot attempted

Three dispatches in one handler

After one click, what does the button show?
function reducer(state, action) {
return { ...state, count: state.count + action };
}
function App() {
const [state, dispatch] = useReducer(reducer, { count: 0 });
const handleClick = () => {
dispatch(1);
dispatch(1);
dispatch(1);
};
return <button onClick={handleClick}>{state.count}</button>;
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • 3
  • Actions can be any value
  • each dispatch(1) is queued and the reducer gets the latest queued state, so the three updates compound to 3.
  • Not 1: reducers don't read stale render values.
  • Not 0: all three apply on the next render.
  • Not 'throws for non-object action': React places no constraint on the action's shape (that is a Redux convention).