Sub topic 10
How dispatch behaves: identity, batching, and bail-out
This chapter assumes you can write a reducer, call useReducer, and dispatch actions from event handlers. It covers three behaviors of dispatch that affect how you write handlers and effects.
Dispatch has a stable identity
useReducer returns the same dispatch function on every render of a component. You can pass it to children, put it in context, or list it as an effect dependency without causing extra work. The React linter knows dispatch is stable, so it does not require you to list it. Listing it is harmless.
This is why the separate dispatch context from the earlier chapter works. The value in CountDispatchContext never changes, so components that only dispatch do not re-render when the state changes.
Dispatch queues work and does not change state immediately
Calling dispatch does not change the state variable in the code that is running. It asks React to run the reducer on the next render. Inside the same handler, state still holds the value from the current render.
React also batches updates. Several dispatches in one event handler are queued, and the reducer runs for each action in order. React then re-renders once. React 18 batches this way in timeouts, promises, and native event handlers too.
import { useReducer } from 'react';
function reducer(state, action) {
switch (action.type) {
case 'incremented':
if (state.count >= 5) return state;
return { count: state.count + 1 };
default:
throw Error('Unknown action: ' + action.type);
}
}
export default function Counter() {
const [state, dispatch] = useReducer(reducer, { count: 0 });
console.log('render, count =', state.count);
function handleClick() {
dispatch({ type: 'incremented' });
dispatch({ type: 'incremented' });
dispatch({ type: 'incremented' });
console.log('inside handler, count =', state.count);
}
return (
<>
<p>Count: {state.count}</p>
<button onClick={handleClick}>+3</button>
</>
);
}On the first click, the handler logs count = 0, because state is a snapshot from the current render. React then renders once with count = 3. A second click goes from 3 to 5, because the reducer returns state unchanged once the count reaches 5.
Because each action runs through the reducer in order, every action sees the result of the one before it. This is why reducers avoid the stale-value problem that setCount(count + 1) has in useState.
Bail-out when the state does not change
After running the reducer, React compares the new state with the old state using Object.is. If they are the same value, React bails out. It does not re-render the children and does not fire effects.
React may still call your component function once more before it bails out. This is expected and does not reach the children, but your component's own code can run an extra time.
Bail-out makes one mistake easy to miss: mutating state.
function brokenReducer(state, action) {
switch (action.type) {
case 'incremented':
state.count += 1; // mutates the existing object
return state; // same reference, so React sees no change
default:
return state;
}
}The reducer changes the object, but returns the same reference. React sees an unchanged state and skips the update, so the screen stays stale. Always return a new object or array when something changed. Return the existing state only when nothing changed.
Summary of the three behaviors
| Behavior | What it means | What to do |
|---|---|---|
| Stable identity | `dispatch` is the same function on every render | Pass it freely. Do not wrap it in `useCallback`. |
| Queued and batched | `state` does not change inside the handler. Several dispatches cause one render. | Do not read `state` right after dispatching. Use the action data instead. |
| Bail-out | A result equal to the old state under `Object.is` skips the update | Return `state` when nothing changed. Never mutate and return it. |
Exercise
A handler calls dispatch({ type: 'incremented' }) and then logs state.count on the next line. The count was 2 before the click. What does the log show, and why? What does the screen show after the re-render?
Show answer
The log shows 2. dispatch only queues the action, and state is a snapshot from the current render. After React runs the reducer and re-renders, the screen shows 3.
Exercise
A reducer handles the 'renamed' action by writing state.name = action.name and then returning state. The input in the UI does not update. Why, and how do you fix it?
Show answer
The reducer returns the same object reference, so Object.is reports no change and React bails out. Return a new object instead: return { ...state, name: action.name };.