ReactMedium

What is useDeferredValue?

useDeferredValue(value, initialValue?) returns a lagging copy of a value. On urgent updates React first re-renders with the old deferred value, then renders again in the background with the new one, interrupting that background render if the value changes again. Use it to keep input responsive while a slow part of the UI catches up.

useTransition vs useDeferredValue

  • useTransition: you control the state update and wrap it in startTransition.
  • useDeferredValue: you receive a value (often a prop or state you do not control) and defer the expensive rendering that depends on it.

Patterns

  • Search as you type: const deferredQuery = useDeferredValue(query) and render results from deferredQuery. The input updates immediately.
  • Show stale content while loading: with Suspense-enabled data, the old results stay visible while new ones load, instead of a fallback flashing.
  • Detect staleness: const isStale = query !== deferredQuery to dim results.
  • Initial value (React 19): useDeferredValue(value, "") renders the initial value first, then the real value in the background, useful for a fast first render.

Requirements

  • The slow component must be memoized (memo, or compiled by the React Compiler), otherwise it re-renders with the urgent update anyway and nothing is gained.
  • It is not debouncing: there is no fixed delay, and on fast devices the deferred render happens almost immediately. For limiting network requests, still debounce.

Pairs well with

<Suspense> and use(promise) for data, and <ViewTransition> animations (deferred updates can trigger them).

Code example

tsx
1import { memo, useDeferredValue, useState } from "react";
2
3const SlowResults = memo(function SlowResults({ query }: { query: string }) {
4 const start = performance.now();
5 while (performance.now() - start < 30) {} // simulate slow rendering
6 return <p>Results for “{query}”</p>;
7});
8
9export function Search() {
10 const [query, setQuery] = useState("");
11 const deferredQuery = useDeferredValue(query); // lags behind while rendering
12 const isStale = query !== deferredQuery;
13
14 return (
15 <div>
16 <input value={query} onChange={(e) => setQuery(e.target.value)} placeholder="Search…" />
17 <div style={{ opacity: isStale ? 0.5 : 1, transition: "opacity 150ms" }}>
18 <SlowResults query={deferredQuery} />
19 </div>
20 </div>
21 );
22}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app