
useDeferredValue Is Not a Debounce: How Transitions Keep Typing Responsive
- كاتب موثّق
- React 19
محتويات المقال
useDeferredValue is "React's built-in debounce", makes typing smooth. Then the same developer uses it for the search API and is surprised that the server logs still show one request per keystroke.
The common misconception
useDeferredValue and startTransition are React's debounce. They hold an update back for some fixed amount of time, so fewer renders, and fewer requests, happen while the user types.
setTimeout. The half that's true: both postpone work the user doesn't need this very millisecond. What differs is what decides when that work runs, and whether it can be stopped once it has started.
What actually happens
Two renders, not a timer
query changes, useDeferredValue(query) makes React render twice. First comes an urgent render with the new query but the old deferred value, so the input shows the keystroke right away. Then React tries a background render with the new value for the slow part.
useDeferredValue causes no fixed delay, and React starts the background render as soon as the first one finishes. On a powerful laptop the list catches up almost instantly. On a slow phone it lags behind in proportion to how slow the device is. You never pick a number, so you never pick the wrong one.
The background render can be thrown away
import { useEffect, useState } from "react";export function TicketSearch({ tickets }: { tickets: Ticket[] }) { const [text, setText] = useState(""); const [query, setQuery] = useState(""); // Debounce: wait 300 ms after the last keystroke useEffect(() => { const id = setTimeout(() => setQuery(text), 300); return () => clearTimeout(id); }, [text]); return ( <> <input value={text} onChange={(e) => setText(e.target.value)} /> {/* 3,000 rows: once this render starts, typing waits */} <TicketRows tickets={tickets} query={query} /> </> );}import { memo, useDeferredValue, useState } from "react";// Skips the urgent render: its props haven't changed yetconst SlowRows = memo(TicketRows);export function TicketSearch({ tickets }: { tickets: Ticket[] }) { 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 }}> <SlowRows tickets={tickets} query={deferredQuery} /> </div> </> );}query, and only the list reads the deferred one. While React renders the list in the background, query !== deferredQuery, so the old results are dimmed instead of pretending to be current.
startTransition is the same idea for state you set
useDeferredValue is for a value you receive, such as a prop or a Hook's result. When you own the setter, you can mark the update yourself with startTransition. The function you pass runs immediately, not later like a setTimeout callback; React just marks every state update made during that call as a Transition.
import { memo, useState, useTransition } from "react";const SlowRows = memo(TicketRows);export function TicketFilter({ tickets }: { tickets: Ticket[] }) { const [text, setText] = useState(""); const [query, setQuery] = useState(""); const [isPending, startTransition] = useTransition(); function handleChange(value: string) { setText(value); // urgent: the input must show it now startTransition(() => setQuery(value)); // the list can wait } return ( <> <input value={text} onChange={(e) => handleChange(e.target.value)} /> {isPending && <small>Updating…</small>} <SlowRows tickets={tickets} query={query} /> </> );}Neither one sends fewer requests
useDeferredValue doesn't by itself prevent extra network requests: a search box that fetches per query still fetches per keystroke. If the expensive work isn't rendering, like a request or an analytics call, a debounce is still the right tool, and React's docs note you can use both together: debounce the request, defer the render. One bonus when the list suspends while loading: with a deferred value, users keep seeing the old results instead of the Suspense fallback.
setTimeout) | useDeferredValue / startTransition | |
|---|---|---|
How to apply it in your project
Find timers that guard renders Search for setTimeout,debounceandthrottlearound state updates. Note which ones only delay a value that a slow component renders.Pick the tool by what's slow If rendering is slow, replace the timer with useDeferredValuefor a value you receive, or two states plusstartTransitionfor state you own. If a request or other side work is the cost, keep the debounce.Make the urgent render cheap Wrap the slow component in memoand pass it primitives or values created outside render, so the urgent pass skips it.Test on a slow CPU Use your browser's performance tools to slow the CPU down, type quickly, and check that the input never waits for the list.
Key takeaways
useDeferredValueandstartTransitionhave no delay: the background render starts as soon as the urgent one ends.Background renders are interruptible, so a keystroke never waits for the list; a debounce only postpones the blocking render. The slow child needs memo, and a Transition can't drive a text input's own state.Deferring doesn't reduce network requests; keep a debounce for those, and combine both when you need to.
التعليقات (0)