Compare memoized and plain components
Memoize a component only at a useful boundary
memo wraps a component so React can skip work on parent updates if its props compare equal. By default each prop is compared with Object.is. This is not a promise that the component never renders. Its local state and context subscriptions still work. Do not confuse a cached component with a cached DOM node.
import React,{useState,useMemo,useCallback,memo,createContext,useContext,Profiler} from 'react';function Summary({title,onOpen}){return <button onClick={onOpen}>Open {title}</button>;}const CachedSummary=memo(Summary);export default function App(){const [tick,setTick]=useState(0);const [message,setMessage]=useState('');const open=useCallback(()=>setMessage('Atlas opened'),[]);return <section><button onClick={()=>setTick(n=>n+1)}>Unrelated update</button><p>{tick}</p><Summary title="Atlas" onOpen={open}/><CachedSummary title="Atlas" onOpen={open}/><p role="status">{message}</p></section>;}Both Open Atlas buttons display the same message. The cached variant can skip work when the parent count changes because the string and callback stay stable. An inline object or new callback would undermine the default comparison. Identical UI output does not establish a performance improvement; measure it.
State and context bypass prop-only comparisons
import React,{useState,useMemo,useCallback,memo,createContext,useContext,Profiler} from 'react';const Density=createContext('comfortable');const Row=memo(function Row({title}){const density=useContext(Density);const [expanded,setExpanded]=useState(false);return <section style={{padding:density==='compact'?4:20}}><button aria-expanded={expanded} onClick={()=>setExpanded(v=>!v)}>{title}</button>{expanded&&<p>Archive details</p>}</section>;});export default function App(){const [density,setDensity]=useState('comfortable');return <Density.Provider value={density}><button onClick={()=>setDensity(d=>d==='compact'?'comfortable':'compact')}>Toggle density</button><Row title="Atlas"/></Density.Provider>;}The row toggles its details and responds to density changes despite stable title props. A memo wrapper must not block necessary UI updates. For complex contexts consider splitting subscriptions or passing smaller derived values, but only where evidence justifies the design.
Custom comparisons must preserve behavior
import React,{useState,useMemo,useCallback,memo,createContext,useContext,Profiler} from 'react';function equalPoints(a,b) { return a.length===b.length && a.every((p,i)=>Object.is(p.x,b[i].x)&&Object.is(p.y,b[i].y));}const Plot=memo(function Plot({points,onInspect}){return <ul>{points.map((p,i)=><li key={i}><button onClick={()=>onInspect(i)}>{p.x}, {p.y}</button></li>)}</ul>;},(a,b)=>a.onInspect===b.onInspect&&equalPoints(a.points,b.points));export default function App(){const [scale,setScale]=useState(1);const [message,setMessage]=useState('');const points=[{x:1,y:2},{x:3,y:4}];const inspect=useCallback(i=>setMessage(`Point ${i+1}, scale ${scale}`),[scale]);return <section><button onClick={()=>setScale(s=>s===1?2:1)}>Toggle scale</button><p>Scale {scale}</p><Plot points={points} onInspect={inspect}/><p role="status">{message}</p></section>;}Clicking the first point reports Point 1, scale 1. Toggle scale and click it again: the message must report scale 2. Comparing only coordinates could retain the old event handler. This comparator is valid only for the fixed ordered coordinate shape and the callback prop; any new relevant prop must be compared. Index keys are limited here to a fixed non-reordered list. Deep comparison on arbitrary data may cost more than rendering and can be incorrect for changed shapes. The React Compiler can automatically optimize many components and calculations; repeat the profiling experiment with an explicitly verified compiler-enabled build separately from the uncompiled baseline. Do not assume both builds produce identical render counts, or remove necessary dependency declarations.
Practice
Add a caption prop to Plot and demonstrate why the comparator must include it. Measure the bounded comparison cost against plain rendering using the profiling checklist. Keep the simpler version if the measured benefit is negligible.