Separate reducer and dispatch
Separate the event from the state calculation
An event handler knows what happened; a reducer knows how that event changes the model. Keep the reducer outside the component, with no DOM changes, timers, requests or external counters. Give it everything it needs through its two inputs. This makes its decisions testable without mounting React.
import React, { useReducer } from 'react';function reservationReducer(state, action) { switch (action.type) { case 'guestRenamed': return {...state, guest: action.name}; case 'materialsToggled': return {...state, materials: !state.materials}; default: throw new Error('Unknown reservation action'); }}export default function App() { const [booking, dispatch] = useReducer(reservationReducer, {guest:'Guest', materials:false}); return <main><label htmlFor="guest">Display name</label> <input id="guest" value={booking.guest} onChange={e => dispatch({type:'guestRenamed', name:e.target.value})} /> <label><input type="checkbox" checked={booking.materials} onChange={() => dispatch({type:'materialsToggled'})} /> Include materials</label> <p>{booking.guest}: {booking.materials ? 'Materials included' : 'No materials'}</p></main>;}Each branch produces a replacement object while preserving unrelated fields. The spread is shallow; nested objects would require copying the changed path. State and action objects are inputs, not storage to modify. The handler reads the input value immediately and packages a string, rather than passing the entire browser event.
Dispatch queues actions, not immediate assignments
import React, { useReducer } from 'react';function groupReducer(count, action) { if (action.type === 'joined') return count + 1; if (action.type === 'reset') return 0; throw new Error('Unknown group action');}export default function App() { const [count, dispatch] = useReducer(groupReducer, 0); function joinPair() { dispatch({type:'joined'}); dispatch({type:'joined'}); console.log('Count in this handler:', count); } return <main><p>Group: {count}</p><button onClick={joinPair}>Join pair</button> <button onClick={() => dispatch({type:'reset'})}>Reset</button></main>;}The two queued actions each calculate from the preceding queued state. Starting at zero, the next committed count is expected to be two, while the console statement in the handler still observes zero. React can batch the updates; do not rely on exact render counts. dispatch has stable identity, accepts one action and returns no state result.
Avoid mutation even when a test looks successful
import React, { useReducer } from 'react';function badReducer(state, action) { if (action.type === 'added') state.seats += 1; return state;}export default function App() { const [state, dispatch] = useReducer(badReducer, {seats:0}); return <main><p>{state.seats}</p><button onClick={() => dispatch({type:'added'})}>Add</button></main>;}Incorrect source for inspection only. Mutation changes the old snapshot, and returning the same object lets React skip an update using Object.is comparison. Development purity checks may expose additional inconsistent behavior. Correct the branch to return {...state, seats: state.seats + 1} and define a policy for unknown actions. Keep logging and other side effects out of the reducer. Practice: explain why two joined events are different from computing a replacement count twice from the same captured snapshot.