Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
useDeferredValue is a Hook that lets you defer updating a part of the UI. It creates a delayed version of a value that React can update later, keeping urgent updates responsive and preventing UI freezing!
UI blocks during expensive operations
UI stays responsive, updates when possible
With useDeferredValue, React can:
First, import useDeferredValue and create a deferred version of your state value that you want to optimize.
Two Values
searchQuery updates immediately, deferredQuery updates when React has time.
Use the deferred value in expensive operations like filtering large lists or complex calculations.
Performance Boost
Expensive filtering uses deferred value, keeping UI responsive!
Use the immediate value for responsive UI elements like input fields, while deferred value powers expensive displays.
Best of Both Worlds
Input stays responsive, list updates when possible!
Show loading states when deferred value is stale to provide visual feedback about pending updates.
Visual Feedback
Show users when updates are pending for better UX!
Experience smooth filtering without UI freezing
function DeferredSearchExample() {
const [searchQuery, setSearchQuery] = React.useState('');
const [items] = React.useState(() => {
// Generate 1000 items for realistic performance test
return Array.from({ length: 1000 }, (_, i) => ({
id: i,
name: `Item ${i + 1}`,
category: ['Electronics', 'Books', 'Clothing', 'Food', 'Toys'][i % 5],
price: Math.floor(Math.random() * 1000) + 10
}));
});
// Create deferred version of search query
const deferredQuery = React.useDeferredValue(searchQuery);
// Check if deferred value is stale
const isStale = searchQuery !== deferredQuery;
// Expensive filtering operation (simulated)
const filteredItems = React.useMemo(() => {
console.log('Filtering items with query:', deferredQuery);
// Simulate expensive operation
const start = Date.now();
while (Date.now() - start < 100) {} // 100ms delay
if (!deferredQuery) return items;
return items.filter(item =>
item.name.toLowerCase().includes(deferredQuery.toLowerCase()) ||
item.category.toLowerCase().includes(deferredQuery.toLowerCase())
);
}, [items, deferredQuery]);
return (
<div className="container">
<h2>Responsive Search with useDeferredValue</h2>
<p>Try typing quickly - the input stays responsive while filtering happens in background!</p>
<div className="search-section">
<input
type="text"
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
placeholder="Search 1000 items..."
className="search-input"
/>
<div className="stats">
<span className="item-count">
Showing {filteredItems.length} of {items.length} items
</span>
{isStale && (
<span className="stale-indicator">
Updating...
</span>
)}
</div>
</div>
<div className="results-grid">
{filteredItems.slice(0, 50).map((item) => (
<div key={item.id} className="item-card">
<div className="item-name">{item.name}</div>
<div className="item-category">{item.category}</div>
<div className="item-price">{'{'}item.price{'}'}</div>
</div>
))}
</div>
{filteredItems.length > 50 && (
<div className="more-items">
... and {filteredItems.length - 50} more items
</div>
)}
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<DeferredSearchExample />);Loading preview...
Search/filter operations on thousands of items that would normally block UI
Charts and graphs that need time to recalculate with new data
Real-time text analysis, syntax highlighting, or markdown parsing
Basic state changes that don't impact performance
User input values or anything needing immediate visual feedback
Use useEffect with loading states for API calls instead
Deferred values update when React has time, not immediately like regular state.
Keeps user interfaces responsive during expensive operations.
Keep immediate value for responsive UI, deferred value for expensive operations.
Works with React's concurrent features for optimal performance.