Split state and dispatch into separate contexts
Split state and dispatch into separate React contexts to spare dispatch-only consumers from state updates.
You keep { state, dispatch } in one context. A button that calls dispatch and reads nothing else re-renders every time the state changes, though nothing it shows depends on the state.
import { createContext, useContext, useMemo, useReducer } from 'react'
const reducer = (state, action) =>
action.type === 'add' ? { count: state.count + 1 } : state
const AppContext = createContext(null)
export function AppProvider({ children }) {
const [state, dispatch] = useReducer(reducer, { count: 0 })
const value = useMemo(() => ({ state, dispatch }), [state, dispatch])
return <AppContext.Provider value={value}>{children}</AppContext.Provider>
}
export function AddButton() {
console.count('AddButton render')
const { dispatch } = useContext(AppContext)
return <button onClick={() => dispatch({ type: 'add' })}>Add</button>
}A Count component next to the button reads state.count from the same context. I mounted both in jsdom and clicked the button five times. Without Strict Mode, the log ends at AddButton render: 6, one render on mount and five more for the clicks.
React compares the context value with Object.is, so every state change makes a new { state, dispatch } object. Every component that calls useContext(AppContext) renders again, including the ones that read dispatch and nothing else. The useMemo does not help, because state is in its dependency list.
Give dispatch its own context
The dispatch function from useReducer keeps the same identity between renders. Put it in a context of its own and a state-only change no longer updates that context. A dispatch consumer can still render for other reasons, such as a parent render.
import { createContext, useContext, useReducer } from 'react'
const reducer = (state, action) =>
action.type === 'add' ? { count: state.count + 1 } : state
const StateContext = createContext(null)
const DispatchContext = createContext(null)
export function AppProvider({ children }) {
const [state, dispatch] = useReducer(reducer, { count: 0 })
return (
<DispatchContext.Provider value={dispatch}>
<StateContext.Provider value={state}>{children}</StateContext.Provider>
</DispatchContext.Provider>
)
}
export function AddButton() {
console.count('AddButton render')
const dispatch = useContext(DispatchContext)
return <button onClick={() => dispatch({ type: 'add' })}>Add</button>
}Count now reads useContext(StateContext) and AddButton reads useContext(DispatchContext). After five clicks, the last console.count lines from each run read:
one context
Count render: 6
AddButton render: 6
two contexts
Count render: 6
AddButton render: 1Count renders six times in both versions, once on mount and once per click, because it shows the state. AddButton drops from six renders to the one on mount.
Where I split
I split when some components write and others read, such as button rows, toolbars and form submit buttons. A component that reads both calls both hooks and renders on every change, as it should.
I wrap the two useContext calls in useAppState() and useAppDispatch(), so components never import the context objects and the split stays inside one file.