Deferring Expensive List Filters
What you'll learn: How to keep search inputs snappy when filtering large lists by deferring the filtering work with useDeferredValue.
The Problem: Heavy Filtering Blocks Input
Imagine you have a searchable list of 10,000 products. Every keystroke filters the entire array, which can take 50-100ms. That delay makes typing feel sluggish—each character appears late because React is busy recalculating what to display.
The Solution: Defer the Filter, Not the Input
With useDeferredValue, you split your state into two values:
- Urgent value: The search input itself (updates immediately)
- Deferred value: The query used for filtering (updates when React has time)
This keeps the text input responsive while the heavy list filtering happens in the background.
How It Works
function SearchableList({ items }) {
const [query, setQuery] = useState('');
const deferredQuery = useDeferredValue(query);
// Input updates query immediately
// Filter uses deferredQuery (may lag behind)
const filtered = items.filter(item =>
item.name.toLowerCase().includes(deferredQuery.toLowerCase())
);
return (
<>
<input
value={query}
onChange={e => setQuery(e.target.value)}
/>
<List items={filtered} />
</>
);
}
The input stays instant because query updates immediately. The expensive filter runs with deferredQuery, which React updates during idle moments. Users see their typing without lag, and results "catch up" milliseconds later.
Visual Feedback
You can detect when values differ:
const isStale = query !== deferredQuery;
// Show opacity or loading indicator when isStale is true
Key Takeaway: useDeferredValue keeps inputs responsive during expensive filtering by letting the filter work use a slightly-behind version of the state, preventing the heavy computation from blocking user typing.