Use useMemo and useCallback appropriately
Cache a pure calculated value
useMemo is a Hook that accepts a pure no-argument calculation and a dependency list. React can reuse the previous value while dependencies compare equal by Object.is. List each reactive input used inside the calculation. Do not rely on the cache to preserve data required for correctness: use state or refs for their appropriate persistence semantics instead. React can discard memoized results, for example on initial suspension.
import React,{useState,useMemo,useCallback,memo,createContext,useContext,Profiler} from 'react';function summarize(rows, query) { const term = query.trim().toLowerCase(); const matches = rows.filter(row => row.title.toLowerCase().includes(term)); return {count: matches.length, pages: matches.reduce((sum,row)=>sum+row.pages,0)};}function makeRows(size) { return Array.from({length:size}, (_,i)=>({id:i,title:i%2===0?'Atlas '+i:'Journal '+i,pages:i%5+1}));}const rows=makeRows(20000);export default function App(){const [query,setQuery]=useState('atlas');const [compact,setCompact]=useState(false);const result=useMemo(()=>summarize(rows,query),[query]);return <section style={{padding:compact?4:20}}><label>Search <input value={query} onChange={e=>setQuery(e.target.value)}/></label><button onClick={()=>setCompact(v=>!v)}>Toggle spacing</button><p>{result.count} records, {result.pages} pages</p></section>;}The visible behavior matches the plain example. rows is a stable module-level fixture; query is the changing input. If rows came from props or state it would also need to be listed. Creating a fresh options object in the component and using it as a dependency defeats reuse; build the object inside the calculation or depend on its individual primitive inputs. Never suppress the dependency linter to keep an outdated calculation.
A callback cache retains a function, not its result
import React,{useState,useMemo,useCallback,memo,createContext,useContext,Profiler} from 'react';const Pick=memo(function Pick({id,onPick}){return <button onClick={()=>onPick(id)}>Select {id}</button>;});export default function App(){const [selected,setSelected]=useState([]);const [compact,setCompact]=useState(false);const pick=useCallback(id=>setSelected(old=>old.includes(id)?old:old.concat(id)),[]);return <section style={{padding:compact?4:20}}><button onClick={()=>setCompact(v=>!v)}>Toggle spacing</button><Pick id="Atlas" onPick={pick}/><Pick id="Journal" onPick={pick}/><p>Selected: {selected.join(', ')||'none'}</p></section>;}Selecting Atlas twice keeps one entry; selecting Journal adds another. useCallback returns a function for the event to invoke later. The updater receives current queued state so this callback does not need to capture selected. The setter has stable identity. If the callback reads changing props or state directly, include those dependencies. A memoized callback alone does not stop child renders; the receiving component must have an optimization that benefits from stable identity.
Diagnose an incomplete dependency list
import React,{useState,useMemo,useCallback,memo,createContext,useContext,Profiler} from 'react';export default function App(){const [label,setLabel]=useState('Atlas');const [message,setMessage]=useState('');const stale=useCallback(()=>setMessage(label),[]);const current=useCallback(()=>setMessage(label),[label]);return <section><label>Label <input value={label} onChange={e=>setLabel(e.target.value)}/></label><button onClick={stale}>Incorrect callback</button><button onClick={current}>Correct callback</button><p role="status">{message}</p></section>;}This diagnostic example deliberately violates dependency correctness in stale. In an ordinary retained cache, change Atlas to Journal and press Incorrect callback: it can still display Atlas because the earlier closure captured that value. Correct callback displays Journal. Cache eviction may alter the diagnostic observation; it is not a guarantee of staleness. In production code remove the broken callback. A plain event handler also works and is preferable when no identity optimization is needed.
Practice
Change the selection callback to enforce a maximum passed as a prop. Include that prop in its dependencies; test lowering the maximum, selecting duplicate IDs and removing memoization without changing the intended behavior.