Avoid unstable provider values where useful
Understand value identity before optimizing
A consumer can update for ordinary parent rendering or because its context value changed. React compares consecutive values for a provider using Object.is. Newly allocated objects are distinct even if their fields match; a memoized consumer still receives context changes. Avoid using render counts as a correctness contract. Start with clear code and only optimize unnecessary work when profiling demonstrates a need.
import React, { createContext, useContext, useState, memo } from 'react';const SettingsContext = createContext(null);function makeSettings(density) { return {density}; }const Label = memo(function Label() { const {density} = useContext(SettingsContext); return <p>Spacing: {density}</p>;});export default function App() { const [count, setCount] = useState(0); const settings = makeSettings('compact'); return <main><button onClick={() => setCount(c => c+1)}>Unrelated clicks: {count}</button> <SettingsContext value={settings}><Label /></SettingsContext></main>;}The counter changes and the spacing label stays compact. Yet the provider gets a fresh object on every App render. memo (a preview of a later lesson) can skip some work for equal props, not a changed context value. Use React DevTools or the Profiler in an approved real runtime to investigate; this example makes no measured performance claim.
Prefer a simple value or a stable object when appropriate
import React, { createContext, useContext, useState, memo } from 'react';const DensityContext = createContext('comfortable');const Label = memo(function Label() { return <p>Spacing: {useContext(DensityContext)}</p>;});export default function App() { const [density, setDensity] = useState('comfortable'); const [count, setCount] = useState(0); return <main><button onClick={() => setCount(c => c+1)}>Unrelated clicks: {count}</button> <button onClick={() => setDensity(d => d === 'compact' ? 'comfortable' : 'compact')}>Toggle spacing</button> <DensityContext value={density}><Label /></DensityContext></main>;}Changing the unrelated counter leaves the context string equal; toggling spacing changes it and updates the label. This avoids an unnecessary context-value change, not every possible render. If an object and callbacks are genuinely required, useMemo and useCallback can sometimes help stabilize them with complete dependencies (covered later). Do not rely on these performance caches for correctness. Never mutate the old object to force identity to stay equal.
Separate the shared model from its update channel
import React, { createContext, useContext, useReducer } from 'react';const DensityContext = createContext(null);const DispatchContext = createContext(null);function settingsReducer(state, action) { switch (action.type) { case 'toggled': return state === 'comfortable' ? 'compact' : 'comfortable'; case 'reset': return 'comfortable'; default: throw new Error('Unknown settings action'); }}function SettingsProvider({children}) { const [density, dispatch] = useReducer(settingsReducer, 'comfortable'); return <DensityContext value={density}> <DispatchContext value={dispatch}>{children}</DispatchContext> </DensityContext>;}function Label() { return <p>Spacing: {useContext(DensityContext)}</p>; }function Controls() { const dispatch = useContext(DispatchContext); if (dispatch === null) throw new Error('SettingsProvider is required'); return <><button onClick={() => dispatch({type:'toggled'})}>Toggle</button> <button onClick={() => dispatch({type:'reset'})}>Reset</button></>;}export default function App() { return <main><h1>Reading room settings</h1><SettingsProvider><Label /><Controls /></SettingsProvider></main>;}Toggle switches the shared setting and Reset restores comfortable. The reducer owns the policy; the state and dispatch contexts transport different values. useReducer dispatch has stable identity, so a dispatch-only reader does not subscribe to density changes through context. It may still render for other reasons. Two providers of SettingsProvider hold independent state. Use splitting when consumers actually have distinct needs; more contexts also increase wiring. Practice: add a second settings provider and prove both labels respond only to their own controls in the browser. Test the pure reducer independently.