Lesson 19 of 37Modern React
Transitions, Suspense and Deferred Values
Keep a React UI responsive under heavy renders and slow data with useTransition, useDeferredValue, and well-placed Suspense boundaries.
A product manager files a bug: "Clicking the Reports tab freezes the page for half a second, and when you search the orders table the whole list flashes to a spinner on every key." Nothing is broken in the usual sense. Every update is correct; it is just treated as equally urgent, so a slow render blocks the next click and every new request throws away the screen the user was reading. React's concurrent features exist for exactly this: you tell React which updates can wait and where loading states belong, and it schedules the rest. This lesson turns the short preview in How Rendering Works into tools you can use on purpose.
Urgent and non-urgent updates
Every state update has a priority. Some updates are urgent: the user typed a letter, pressed a button, opened a menu. If those do not show up within roughly 100 ms, the app feels broken. Other updates are non-urgent: showing the results of that typing, rendering the tab that was clicked, swapping in the next page of data. Users expect those to take a moment, as long as the urgent feedback arrives first.
By default React treats every setState as urgent and renders it in one go. A transition marks an update as non-urgent. React renders it in the background on a work-in-progress tree (the Fiber section of How Rendering Works explains why that is possible), yields to the browser every few milliseconds, and throws the half-done work away if an urgent update arrives. Nothing on screen changes until the transition render finishes and commits.
Two consequences follow, and both come up in code review:
- A transition never makes the work cheaper. Rendering 2,000 rows still costs what it costs. It only stops that work from blocking input.
- The value of a controlled
<input>must never be set inside a transition. The input would wait for the slow render and feel stuck. Keep the input update urgent and put the expensive consequence in the transition.
useTransition: mark your own updates
const [isPending, startTransition] = useTransition();
function selectTab(next: Tab) {
startTransition(() => {
setTab(next);
});
}startTransition calls your function immediately and synchronously. Every state update made during that call is marked as a transition. isPending is true from the moment you start until the transition commits, which lets you dim the old content or show a small spinner on the clicked tab without replacing anything. If you need a transition outside a component (in a router or a store), import the standalone startTransition from react; it works the same but has no isPending.
Try it. The Posts tab renders 200 deliberately slow items. With the transition on, click Posts and then Contact straight away: Contact appears quickly because React abandons the Posts render. Turn the transition off and the same clicks queue up behind a frozen page.
Async transitions
Since React 19 the function you pass can be async. React keeps isPending true for the whole function, including the time spent awaiting a request, which is the basis of the Actions you will meet in the next lesson.
There is one trap that catches almost everyone. State updates that run after an await are not automatically part of the transition, because JavaScript has no way yet for React to follow the async context across the await. Wrap them again:
startTransition(async () => {
const report = await loadReport(id);
startTransition(() => {
setReport(report); // without this wrapper, this update is urgent
});
});The React docs call this a known limitation that should go away once the platform gives libraries a way to track async context. Until then, a linter will not save you, so it is worth checking in review.
useDeferredValue: defer a value you do not own
useTransition needs access to the setState call. Often you do not have it: the search text arrives as a prop, or it comes from a store. useDeferredValue is the value-side version of the same idea.
const deferredQuery = useDeferredValue(query);On an urgent render, deferredQuery keeps its old value, so the expensive part of the tree can skip work (it must be wrapped in memo, or the parent re-render redoes the work anyway). React then starts a background render with the new value. If query changes again before that render finishes, React abandons it and starts over with the newest value. Comparing query !== deferredQuery tells you the screen is stale, which is a good moment to dim it.
React 19 added a second argument, an initial value: useDeferredValue(query, ""). On the first render the hook returns "", so the component can show something cheap immediately, then React renders again in the background with the real value.
Instead of debouncing
The classic fix for search-as-you-type is a debounce: wait 300 ms after the last keystroke, then filter. The deferred value is usually better for rendering work. A debounce waits the same 300 ms on a fast laptop and a slow phone. A deferred render starts at once, finishes as fast as the device allows, and is interrupted by the next keystroke, so fast devices feel instant and slow ones stay responsive.
It does not reduce network requests, though. Every keystroke can still start a fetch. If each request costs money or load on the server, keep a debounce (or a request cache) for the network and use the deferred value for the rendering.
One caveat from the docs: pass primitives or objects created outside render. useDeferredValue({ query }) creates a new object every render, so the deferred value never settles and you pay for a background render each time.
Suspense boundaries: what suspends and what does not
<Suspense fallback={...}> shows its fallback while anything inside it is suspended: waiting for something it needs before it can render. Only certain sources can suspend:
lazy()components while their code downloads (you used this in Performance).use(promise)on a promise that is still pending (from Data Fetching).- Suspense-enabled data layers: async Server Components in Next.js, TanStack Query's
useSuspenseQuery, Relay.
Data you load in a useEffect or an event handler does not suspend. Suspense cannot see it, so you are back to manual loading state. This trips people up when they wrap an effect-fetching component in <Suspense> and wonder why the fallback never shows.
The nearest boundary above a suspended component is the one that shows its fallback. Boundaries nest, and that gives you control over the reveal order:
The header does not suspend, so it renders straight away whatever the data is doing.
Put boundaries where a loading state makes sense to the user, not around every component. Ten small boundaries that resolve at random times give you popcorn UI: pieces popping in one by one, the layout jumping each time. Group content that belongs together under one boundary, make fallbacks the same size as the content they stand in for, and let optional content (recommendations, comments) load last behind its own boundary. React also throttles reveals slightly, so boundaries that resolve close together tend to appear in one step instead of flickering in separately.
React 19 changed how suspended siblings render
In React 18, when one child suspended, React kept rendering its siblings before showing the fallback. In React 19, React commits the fallback immediately when a component suspends, then renders the remaining siblings in the background to "pre-warm" them so their requests start.
The fallback appears sooner, which is good. The catch is for code that only starts fetching when a component renders: if sibling A suspends and sibling B starts its own fetch in render, B's request now starts a little later. The fix is the same one that avoids waterfalls in general: start requests as early as possible (in a route loader, an event handler, a parent, or a Server Component) and pass promises down, rather than starting them deep in the tree during render.
Why a transition keeps the old UI
Here is the rule that makes everything above fit together. If an update is urgent and it causes an already visible boundary to suspend, React hides the content and shows the fallback. If the update is a transition, React refuses to hide content the user can already see. It keeps the old UI on screen, sets isPending, and waits until the new content is ready.
That is why routers wrap navigation in transitions. Clicking a link in a Next.js app keeps the current page visible (with a pending indicator) while the next one loads, instead of flashing a full-page skeleton for 200 ms. A boundary that is new in the next screen still shows its fallback, because there was nothing on screen to protect.
The handler calls startTransition(() => setPage(2)).
Sometimes you want the fallback, for example when navigating from one user's profile to another's, where showing the old user's data while the new one loads would be misleading. Give the boundary (or the component above it) a key: <Suspense key={userId} fallback={...}>. A new key means new content, so React shows the fallback even inside a transition.
The demo below puts it together. The input stays urgent. The results read a cached promise with use, so they suspend. With the deferred value on, typing keeps the previous results dimmed while new ones load. Turn it off and every keystroke replaces the list with the fallback.
The deferred background render behaves like a transition, so it follows the same rule: if the background render suspends, the user keeps seeing the old value.
Page 1 is on screen. The user clicks Next. Page 2's promise takes one second. What does the user see during that second?
function Pager() {
const [page, setPage] = useState(1);
const [isPending, startTransition] = useTransition();
return (
<>
<button onClick={() => startTransition(() => setPage(page + 1))}>Next</button>
<Suspense fallback={<p>Loading...</p>}>
<PageResults page={page} /> {/* calls use(fetchPage(page)) */}
</Suspense>
</>
);
}Promises for use must be stable
Data Fetching showed that use(fetch(...)) written straight in a component body never settles: every retry creates a fresh promise. At work the fix is choosing who owns the promise:
- A framework or library creates and caches it (Next.js Server Components passing a promise to a Client Component, TanStack Query, a router loader).
- A cache keyed by input, like the
Mapin the demo above. Remember that a cache also needs an eviction story, or a rejected promise stays rejected forever. Error Boundaries returns to this. - An event handler creates it and stores it in state inside a transition:
startTransition(() => setReportPromise(loadReport(id))). The promise is created once per click, not once per render.
Never create it during render without a cache, and do not reach for an effect to create it; that brings back the waterfall Suspense was meant to remove.
ProductGrid receives filterText as a prop from a parent you do not control. Re-rendering the grid takes 250 ms. Which tool fits best?Match each situation to the tool that handles it.
With useDeferredValue, the user types the letter r into a search box. Put what happens in order.
- Tap the steps below in the right order.
- transition
- A state update marked as non-urgent with
startTransition. React renders it in the background, can interrupt it, and does not hide visible content for it. - isPending
- The flag from
useTransitionthat is true from the start of a transition until it commits, including awaited time in an async transition. - useDeferredValue
- A hook that returns a lagging copy of a value. Urgent renders see the old value; React renders the new one in an interruptible background pass.
- Suspense boundary
- A
<Suspense fallback>component that shows its fallback while something inside it, such aslazycode oruse(promise), is not ready. - reveal order
- The sequence in which nested Suspense boundaries swap fallbacks for content. You design it by where you place boundaries.
- popcorn UI
- Many small loading states resolving one by one, making content pop in and the layout jump. Fixed by grouping content under fewer boundaries.