Defer a slow results view
Defer a value rather than owning two setters
Sometimes the component receives a changing value from props or owns one search field and wants a slower consumer to follow it. useDeferredValue returns a value React may temporarily keep at the previous version while trying a new render. Unlike a timer, it imposes no fixed wait. In React 18+ pass a primitive or stable object; this lesson uses strings and does not require the optional initialValue introduced in React 19.
import React,{useState,useTransition,startTransition,useDeferredValue,memo,Profiler} from 'react';const records=Array.from({length:600},(_,i)=>({id:i+1,name:`${i%2===0?'Cedar':'Birch'} sample ${i+1}`}));function searchRecords(query,items=records){const term=query.trim().toLowerCase();return items.filter(item=>item.name.toLowerCase().includes(term));}function moveIndex(index,delta,length){return Math.max(0,Math.min(length-1,index+delta));}const Results=memo(function Results({query}){ const matches=searchRecords(query); return <section><p>{matches.length} matches</p><ul>{matches.map(item=><li key={item.id}>{item.name}</li>)}</ul></section>;});export default function App(){const [query,setQuery]=useState('');const deferred=useDeferredValue(query);const stale=query!==deferred; return <section><label>Find samples <input value={query} onChange={e=>setQuery(e.target.value)}/></label><p role="status">{stale?'Updating; showing previous results':'Results current'}</p><div aria-busy={stale}><Results query={deferred}/></div></section>;}Typing birch eventually shows 300 matches. The old list may remain visible briefly while the new query is rendered. The text explains why results may not match the input; no color-only cue is needed. The memo boundary matters: otherwise a parent update can run the list again even with unchanged deferred props. A changing object constructed on every render would also defeat this comparison. With a value already updated in a transition, React need not introduce a second deferred render. Effects for abandoned render attempts are not committed.
Compare the same work with and without deferral
import React,{useState,useTransition,startTransition,useDeferredValue,memo,Profiler} from 'react';const records=Array.from({length:600},(_,i)=>({id:i+1,name:`${i%2===0?'Cedar':'Birch'} sample ${i+1}`}));function searchRecords(query,items=records){const term=query.trim().toLowerCase();return items.filter(item=>item.name.toLowerCase().includes(term));}function moveIndex(index,delta,length){return Math.max(0,Math.min(length-1,index+delta));}const Results=memo(function Results({query}){ const matches=searchRecords(query); return <section><p>{matches.length} matches</p><ul>{matches.map(item=><li key={item.id}>{item.name}</li>)}</ul></section>;});export default function App(){const [query,setQuery]=useState('');const [defer,setDefer]=useState(true);const deferred=useDeferredValue(query);const shown=defer?deferred:query; function report(id,phase,actualDuration){console.log({id,phase,actualDuration});} return <section><label><input type="checkbox" checked={defer} onChange={e=>setDefer(e.target.checked)}/> Defer results</label><label>Search <input value={query} onChange={e=>setQuery(e.target.value)}/></label><p role="status">{shown!==query?'Updating results':'Current results'}</p><Profiler id="sample-results" onRender={report}><Results query={shown}/></Profiler></section>;}Use the same catalogue, browser, build mode, CPU settings and input sequence in both runs. Record input-to-paint delay separately from Profiler render duration; neither one alone describes the full experience. Use a profiling-capable build, repeat trials, discard warm-up runs and report distributions. The fixture has bounded 600-row data and may be too fast to reveal a benefit. The included Node benchmark measures filtering only; it cannot measure React scheduling, paint or interaction latency. Do not claim a speedup from this experiment until UI measurements exist.
Deferral is not request control
import React,{useState,useTransition,startTransition,useDeferredValue,memo,Profiler} from 'react';export default function App(){const [query,setQuery]=useState('');const [submitted,setSubmitted]=useState('');const deferred=useDeferredValue(query); return <section><form onSubmit={e=>{e.preventDefault();setSubmitted(query);}}><label>Sample name <input value={query} onChange={e=>setQuery(e.target.value)}/></label><button>Apply</button></form><p>Preview term: {deferred||'All'}</p><p role="status">Applied term: {submitted||'All'}</p></section>;}The preview follows the input at rendering priority; Apply captures the current text immediately. No network request is made. For remote search, use an explicit request policy such as debounce, cancellation and cache keys. Deferred rendering alone does not reduce requests or impose a fixed delay. Practice: run the two comparison modes using identical input, record final results, and report perceived latency and render duration separately.