Part 10 / Topic 6 of 8
dispatch's identity is stable across renders
React guarantees dispatch keeps the exact same function identity for as long as a component instance lives - it is not recreated on re-render the way a function you define directly inside the component body is. examples/05_dispatch_stability.jsx captures both dispatch and a plain inline handler on every render, through a callback prop a test can read from the outside:
// examples/05_dispatch_stability.jsx
export function DispatchStabilityDemo({ onObserve }) {
const [, dispatch] = useReducer(cartReducer, initialCart);
const [tick, setTick] = useState(0);
const inlineForceRerender = () => setTick((current) => current + 1);
onObserve({ dispatch, inlineForceRerender });
// ...
}
Rendering this component, forcing a rerender by clicking its button, and comparing both captured values with === really shows dispatch from the first render is the exact same function object as dispatch from the second - while inlineForceRerender's two captures are two different function objects (examples/05_dispatch_stability.test.jsx). This is more than trivia: it is exactly what makes dispatch safe to put in a useEffect dependency array (Part 6) or pass as a prop into a React.memo-wrapped child (Part 12) without ever causing an extra effect run or an extra render on that account alone - something a fresh inline handler cannot promise.
Done here? The practice set for Part 10 — including the solutions — is on the part page.