Sub topic 7
Deferring a value with useDeferredValue
This chapter assumes you know React function components, useState, props, and how a parent re-render re-renders its children. Familiarity with React.memo helps. You should also have seen useTransition from the earlier chapters.
What useDeferredValue does
useDeferredValue(value) returns a copy of a value that can lag behind the original. When the value changes, React first re-renders with the old copy, so urgent work such as typing finishes quickly. It then starts a second render in the background with the new value. If another change arrives while that background render is running, React abandons it and starts over with the latest value.
Use it when you do not control the code that sets the state. With useTransition you wrap the setState call. With useDeferredValue you wrap the value you read. This is useful when the value arrives as a prop or from a library.
A worked example
The input below updates query on every keystroke. The slow list receives the deferred copy.
import { useState, useDeferredValue, memo } from 'react';
const SlowList = memo(function SlowList({ query, items }) {
const visible = items.filter((item) => item.includes(query));
return (
<ul>
{visible.map((item) => (
<li key={item}>{item}</li>
))}
</ul>
);
});
function App({ items }) {
const [query, setQuery] = useState('');
const deferredQuery = useDeferredValue(query);
const isStale = query !== deferredQuery;
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<div style={{ opacity: isStale ? 0.6 : 1 }}>
<SlowList query={deferredQuery} items={items} />
</div>
</>
);
}Follow what happens when the user types a letter:
querychanges. React rendersAppwith the newquerybut the olddeferredQuery.- The input shows the new letter right away.
isStaleistrue, so the list dims. - React starts a background render where
deferredQueryequalsquery. - When that render finishes, the list updates and
isStalebecomesfalse.
Why the child must be memoized
The first render, the one with the old deferred value, passes the same query and items to SlowList as before. Without memo, SlowList would still re-render because its parent did, and the urgent render would be as slow as before. With memo, React compares the props, sees no change, and skips the list. Only the background render, which has a new prop, does the slow work.
Showing that content is stale
The deferred value differs from the real value only while a background render is pending. Comparing the two, as in query !== deferredQuery, gives you a free stale flag. You can use it for dimming, as above, or for a small spinner. Unlike isPending from useTransition, this flag is not provided by React. You derive it yourself.
Things to know
- No fixed delay. The deferral is not a timer. On a fast device the background render can finish so quickly that you never notice the lag.
- Interruptible. Because the background render can be abandoned, a slow list never blocks typing, even if the user types faster than the list can render.
- Initial render. On the first render, the deferred value equals the value you pass. React 19 adds an
initialValueoption, covered in a later chapter. - Pass primitives or stable objects. If you pass an object created during render, it is a new object each time, so the deferred copy never matches the original. React will keep re-rendering in the background.
- It does not reduce work. It reorders work. Total rendering time stays the same, so a list that is too large may still need virtualization.
Exercise
In the example, a teammate removes memo from SlowList. Typing still updates the input, but it now feels laggy again. Why does removing memo cause this?
Show answer
Without memo, SlowList re-renders whenever App re-renders. The urgent render, which uses the old deferredQuery, therefore still runs the slow filter and list rendering before the input can update. With memo, the unchanged props let React skip SlowList in that first render, so only the background render pays the cost.
Exercise
How can you tell in code that a deferred render is still pending?
Show answer
Compare the original value with the deferred one, for example const isStale = query !== deferredQuery;. They differ only while React is still rendering with the old deferred value.