Sub topic 13
The initialValue option in React 19
This chapter assumes you know useState, props, and React.memo. It also assumes you have read the earlier chapters on useDeferredValue and why the child that receives a deferred value must be memoized.
The problem: the first render has no earlier value
useDeferredValue(value) returns the previous value while React renders the new one in the background. On the very first render there is no previous value. In React 18, the hook returns the value you passed in. If the component that uses the deferred value is slow, the first render is slow too, and the deferral gives you nothing until the next update.
React 19 adds a second argument for this case:
const deferredValue = useDeferredValue(value, initialValue);initialValue is the value the hook returns during the first render of the component. React then schedules a background re-render with the real value. This second render can be interrupted, like any other deferred render.
How it behaves
- On mount, the hook returns
initialValue. The component renders quickly with that value. - React commits that result to the screen.
- React starts a second render with the real
value, as a non-urgent update. - If the user does something urgent in the meantime, React interrupts the second render and handles the urgent update first.
Example: a fast first paint for a search page
Suppose a search page opens with a query already filled in, for example from the URL. Rendering the full result list for that query is slow. You can show an empty list first, then fill it in.
import { useDeferredValue, memo } from 'react';
const Results = memo(function Results({ query, items }) {
const visible = items.filter((item) => item.includes(query));
return (
<ul>
{visible.map((item) => (
<li key={item}>{item}</li>
))}
</ul>
);
});
function SearchPage({ initialQuery, items }) {
const deferredQuery = useDeferredValue(initialQuery, '');
const isLoading = deferredQuery !== initialQuery;
return (
<div>
<h1>Results for "{initialQuery}"</h1>
{isLoading && <p>Loading results...</p>}
<Results query={deferredQuery} items={items} />
</div>
);
}On the first render, deferredQuery is ''. The heading and the loading text appear at once. In the background, React renders Results with the real query and replaces the list when it is done. The memo wrapper matters here for the same reason as in earlier chapters. React can only skip work if the child's props do not change between renders that use the old value.
Rules and gotchas
initialValueonly affects the first render of that component instance. Later updates use the normal deferred behavior.- If you omit
initialValue, the hook behaves as in React 18. - You need React 19 or later. In React 18, the second argument is ignored.
- The first render shows
initialValue, so choose a value that makes sense on screen. An empty string or an empty list is common. A value that looks like real data can mislead users. - On the server, the first render also uses
initialValue. The real value is rendered later on the client. Check that your server output looks acceptable withinitialValue. - It does not help when the slow part is not under the deferred value. Only components that read the deferred value render with
initialValue.
When to use it
Use initialValue when a slow subtree depends on a value that is already known on mount, and you want the rest of the page to appear first. For updates after mount, useDeferredValue without the second argument already does the job, and so does useTransition.
Exercise
A component calls useDeferredValue(query, '') where query is "react". What does the deferred value equal on the first render, and what happens right after that render is committed?
Show answer
On the first render the deferred value is the empty string ''. After React commits that render, it starts a background render with the real value "react". If an urgent update arrives, React interrupts the background render and handles the urgent update first.
Exercise
Your app uses React 18. You add useDeferredValue(query, ''). What changes compared with useDeferredValue(query)?
Show answer
Nothing. React 18 ignores the second argument, so the first render uses the real query value. You need React 19 for initialValue to work.