Sub topic 13
The initialValue option in React 19
This chapter covers the second argument that React 19 added to useDeferredValue. It assumes you know useDeferredValue from earlier in this tutorial, including why the child that receives the deferred value should be wrapped in memo. It also assumes you are comfortable with useState and props, and that you have a basic idea of how Suspense works.
The problem: the first render has nothing to defer
In React 18, useDeferredValue(value) lags behind value only on updates. On the first render there is no previous value to return, so the hook returns value itself. The component renders everything at full priority.
That is a problem when a page mounts with an expensive child already driven by a non-empty value. Examples are a search page opened from a link like /search?q=react, or a dashboard that opens with a saved filter. The first paint has to wait for the slow child, and the user sees a blank screen or a spinner for the whole page.
You want the fast parts of the page (the input, the header, the layout) on screen first. The expensive part should then fill in without blocking anything.
What initialValue does
In React 19, useDeferredValue accepts an optional second argument:
const deferredValue = useDeferredValue(value, initialValue);When you pass initialValue, React behaves like this:
- On the initial render of the component, the hook returns
initialValue, notvalue. - React then schedules a background re-render with the real
value. This re-render is not urgent, so it can be interrupted, like any deferred render. - On every later update,
initialValueis ignored. The hook works as it did in React 18: it returns the previous value while a background render with the new value is in progress.
If you omit initialValue, nothing changes from React 18.
The initial value is a render-time substitute. It is not state, and your component never sets it. It only decides what the deferred value is during the first render of that component instance.
The diagram below shows the order of events.
A worked example
This page receives an initialQuery prop, for example parsed from the URL. The results list is slow, so it is memoized and receives only the deferred query.
import { useState, useDeferredValue, memo } from 'react';
const Results = memo(function Results({ query, items }) {
if (query === '') {
return <p>Loading results...</p>;
}
const visible = items.filter((item) => item.includes(query));
return (
<ul>
{visible.map((item) => (
<li key={item}>{item}</li>
))}
</ul>
);
});
export function SearchPage({ initialQuery, items }) {
const [query, setQuery] = useState(initialQuery);
const deferredQuery = useDeferredValue(query, '');
const isStale = query !== deferredQuery;
return (
<>
<h1>Search</h1>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<div style={{ opacity: isStale ? 0.6 : 1 }}>
<Results query={deferredQuery} items={items} />
</div>
</>
);
}Follow what happens when the page mounts with initialQuery="react":
queryis"react", butdeferredQueryis""on the first render.Resultsrenders its cheap placeholder branch.- The heading, the input and the placeholder commit quickly. The user can already click into the input and type.
- React then renders again in the background with
deferredQueryequal to"react". The expensive filter runs, and the list replaces the placeholder when the render finishes. - Once the two values match,
isStalebecomesfalse.
Typing after that works as in the earlier chapters. query updates immediately, and deferredQuery trails it.
Why the placeholder branch is needed
The empty string is only a sensible initial value if your child can handle it. In this example, "".includes would match every item, so an unguarded Results would render the whole unfiltered list on the first paint. That is often the most expensive render of all. Choose an initialValue that makes the child cheap, and make the child handle that value explicitly.
Using isStale on the first render
On the first render, query !== deferredQuery is true whenever initialValue differs from value. The isStale check therefore covers the initial load as well as updates. You can use it to dim the content or show a skeleton without a separate "is mounted" flag.
When the background render does not finish
The deferred render with the real value is a normal low-priority render. Two consequences follow from that.
It can be interrupted. If the user types before the background render completes, React abandons it and starts a new one with the latest value. The input stays responsive throughout.
It can suspend. If the memoized child reads data that is not ready, for example with use on a promise, the background render suspends. React does not show a Suspense fallback for a deferred update. It keeps displaying what is already on screen, which here is the render with initialValue. When the data resolves, React commits the render with the real value.
This makes initialValue a good fit for a pattern where the page shell appears instantly and a data-dependent section fills in later. The cheap initialValue render acts as the visible placeholder.
import { Suspense, use, useDeferredValue } from 'react';
function Albums({ query, albumsPromise }) {
if (query === '') {
return <p>Loading albums...</p>;
}
const albums = use(albumsPromise);
return (
<ul>
{albums.map((album) => (
<li key={album.id}>{album.title}</li>
))}
</ul>
);
}
export function AlbumSection({ query, albumsPromise }) {
const deferredQuery = useDeferredValue(query, '');
return (
<Suspense fallback={<p>Loading...</p>}>
<Albums query={deferredQuery} albumsPromise={albumsPromise} />
</Suspense>
);
}Here the promise comes from a parent, such as a data layer or a cache. Creating a new promise inside a component on every render would loop forever. The Albums branch for query === '' returns before use runs, so the first render never touches the promise.
Behavior to know before you rely on it
It applies once per mount
initialValue is used only when the component instance first renders. If the component stays mounted and value changes later, the hook defers as usual and does not return initialValue again. If the component unmounts and mounts again, or if its key changes, it is a new instance, so initialValue is used again.
The hook returns it even when the two values are equal
If you pass initialValue equal to value, the first render returns that value and no extra background render is needed in practice. The option only changes behavior when the two differ.
It is a render-time value, not a default for missing data
Do not use initialValue to mean "the value when the user has not typed anything". That is the job of your useState initializer. initialValue is about what the deferred copy shows during the first render. It usually should be a cheap value the child can render quickly, and it usually differs from the real value.
Two renders on mount
With initialValue, a component with a differing value renders at least twice on mount: once with the initial value and once in the background with the real value. Effects in the child run for both committed renders. If the child fires a request in an effect keyed by the query, you may trigger a request for "" before the real one. Guard against that by checking for the initial value in the effect, or avoid fetching in effects for this case.
Server rendering
If you render on a server, the markup the server produces uses initialValue for the deferred value, and the real value arrives in a background render after hydration. This lets the server send a lighter page. It also means the server markup shows the placeholder, not the final results, so the HTML does not include content derived from value. Check that this is acceptable for search engines and for users with slow scripts.
When to reach for initialValue
Use it when all of these hold:
- The component mounts with a value that drives an expensive subtree.
- You can describe a cheap render for that subtree, such as a placeholder or an empty state.
- It is acceptable for the real content to appear a moment after the shell.
Skip it when the first paint already needs the real content, for example when the content is the reason the page exists and a placeholder would only add a flash. Skip it as well when the child cannot render a cheap version of itself.
| Situation | Use initialValue? | Reason |
|---|---|---|
| Search page opened with a query in the URL | Yes | Shell and input appear first, results fill in |
| Component that only updates after user input | No | The first render has no expensive work to defer |
| Child cannot render an empty or placeholder state | No | The initial render would be as slow as before |
| Page where content must be in the server HTML | Usually no | The server markup would contain the placeholder |
Exercise
A component calls useDeferredValue(filter, 'all'). It mounts with filter equal to "errors". Later, the user changes filter to "warnings". What does the hook return on the first render, and what does it return in the render right after the user changes the filter?
Show answer
On the first render it returns "all", the initial value. React then schedules a background render in which it returns "errors". When the user changes the filter to "warnings", initialValue is no longer used. The hook returns the previous deferred value, "errors", while React renders "warnings" in the background, and then returns "warnings" when that render completes.
Exercise
Your SearchPage uses useDeferredValue(query, '') and passes the result to a memoized Results component, which filters a large array with item.includes(query). On mount with initialQuery="react", the page is still slow to appear. What is the likely cause and how do you fix it?
Show answer
With an empty string, item.includes('') is true for every item, so Results renders the entire unfiltered list on the first render, which is the most expensive result. Fix it by making Results return a cheap placeholder (or an empty list) when query === '', so the first render is fast and the filtered list renders in the background render.