Skip to content

QuizNot attempted

Dispatching an unknown action

What happens when the button is clicked?
function reducer(state, action) {
switch (action.type) {
case 'set': return action.payload;
default: throw new Error('Unknown action');
}
}
function App() {
const [state, dispatch] = useReducer(reducer, 0);
return (
<button onClick={() => dispatch({ type: 'reset' })}>
{state}
</button>
);
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Throws an "Unknown action" error during render
  • 'reset' matches no case, so the reducer hits default and throws
  • React runs reducers during render, so the error surfaces as a render error (caught by an Error Boundary if one exists).
  • Not 'renders 0 silently': the throw isn't swallowed.
  • Not undefined: the reducer never returns.
  • Not 'click ignored': dispatch always queues the action.