Skip to content

QuizNot attempted

Reducer returns the same state

Dispatching { type: 'noop' } (which returns the *same* state object via default) — does the component re-render, and does 'reducer called with noop' log?
function reducer(state, action) {
console.log('reducer called with', action.type);
switch (action.type) {
case 'inc': return { count: state.count + 1 };
default: return state;
}
}
function App() {
const [state, dispatch] = useReducer(reducer, { count: 0 });
return (
<button onClick={() => dispatch({ type: 'noop' })}>
{state.count}
</button>
);
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Logs, but component does NOT re-render (same reference returned, bails out)
  • React calls the reducer while re-rendering App, so it logs
  • it returns the same state reference, so React bails out: no children re-render and nothing is committed.
  • Not 're-renders': an identical state reference means no committed update.
  • Not 'doesn't log': the reducer must run to know the result is unchanged.
  • Not 'throws': default returns state, it doesn't throw.