Capítulo 52 de 116

Chapter 52: useDeferredValue

Core Idea

useDeferredValue(value, initialValue?) returns a version of value that React is allowed to update "later" (at lower priority) than the value itself, letting an expensive-to-render child lag slightly behind an urgent input like typing, instead of blocking it.

Key Concepts

  • Signature: const deferredValue = useDeferredValue(value, initialValue?). On the very first render, deferredValue equals initialValue if provided, otherwise value. On updates, React first tries to re-render with the old deferred value, then schedules a second, lower-priority re-render with the new one.
  • Typical use: an input's live value stays perfectly responsive (typed characters appear immediately) while a heavy list/result rendered from that value is wrapped so it can visibly lag one render behind, keeping the UI from janking on every keystroke.
  • Not the same as debouncing/throttling: there's no fixed delay — React updates the deferred value as soon as it can fit the work in without blocking more urgent renders, which can be near-instant on a fast device and noticeably longer on a slow one.
  • Combine with memo for the real payoff: deferring the value alone doesn't skip re-rendering the child that consumes it; wrapping that child in memo (Ch 88) lets React actually skip re-rendering it while the deferred value hasn't caught up yet, since its props haven't changed.
  • Showing a stale-state indicator: since deferredValue !== value while the deferred update is pending, that comparison can drive a visual cue (reduced opacity, a subtle "updating" indicator) on the lagging content.

Code Examples

function SearchPage({ query }) {
  const deferredQuery = useDeferredValue(query);
  return <SearchResults query={deferredQuery} />; // wrap SearchResults in memo for full benefit
}
  • What it demonstrates: the input (query) stays instantly responsive while SearchResults receives a value React is free to update slightly later.

Key Takeaways

  1. useDeferredValue protects urgent rendering (typing, direct input feedback) from being blocked by expensive downstream rendering — it doesn't make the downstream rendering itself faster.
  2. Pairing with memo on the consuming component is what actually lets React skip wasted renders while waiting to catch up — without it, the deferred value still causes a re-render, just a lower-priority one.
  3. This is a performance tool for a specific "urgent input, expensive derived UI" shape — not a general substitute for useState.

Connects To

  • Ch 88 (memo): needed alongside this Hook to actually skip re-renders of the lagging child.
  • Ch 65 (useTransition): the related tool for marking an update itself (not a derived value) as lower priority.