Performance: Context provider
MEDIUM IMPACT
This affects interaction responsiveness and rendering speed by controlling how many components re-render when context changes.
const MyContext = React.createContext(); function App() { const [count, setCount] = React.useState(0); const value = React.useMemo(() => ({ count, setCount }), [count, setCount]); return ( <MyContext.Provider value={value}> <ComponentA /> <ComponentB /> </MyContext.Provider> ); }
const MyContext = React.createContext(); function App() { const [count, setCount] = React.useState(0); const value = { count, setCount }; return ( <MyContext.Provider value={value}> <ComponentA /> <ComponentB /> </MyContext.Provider> ); } // ComponentA and ComponentB consume context // but value object is recreated on every render
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Context value recreated every render | Many re-renders of consumers | Multiple reflows if layout changes | High paint cost due to frequent updates | [X] Bad |
| Memoized context value with stable reference | Re-renders only on actual value change | Minimal reflows | Lower paint cost | [OK] Good |