Transitions and deferred values
Overview
Sometimes an update is unavoidably expensive: filtering and rendering thousands of rows, drawing a complex chart, switching to a heavy tab. React normally renders updates synchronously and to completion, so while that work runs, the browser can’t respond to the next keystroke — typing stutters and clicks feel ignored.
React’s concurrent features fix this by letting you mark some updates as non-urgent. React works on them in the background, and drops or restarts them when something more urgent happens.
Urgent and non-urgent updates
Think about what users expect:
- Urgent: the text they type appears in the input; the checkbox they click ticks; the button they press looks pressed. These must happen instantly.
- Non-urgent: the search results update; the next tab’s content appears; a chart redraws. A short delay is fine — as long as the urgent stuff isn’t blocked.
By default React treats every update as urgent. Transitions let you say “this one can wait”.
useTransition
Wrap a state update in startTransition to mark it as non-urgent:
import { useState, useTransition } from 'react';
function CourseTabs() {
const [tab, setTab] = useState('overview');
const [isPending, startTransition] = useTransition();
function selectTab(nextTab) {
startTransition(() => {
setTab(nextTab);
});
}
return (
<>
<TabButton isActive={tab === 'overview'} onClick={() => selectTab('overview')}>Overview</TabButton>
<TabButton isActive={tab === 'lessons'} onClick={() => selectTab('lessons')}>Lessons</TabButton>
<TabButton isActive={tab === 'reviews'} onClick={() => selectTab('reviews')}>Reviews</TabButton>
{isPending && <Spinner size="small" />}
<div style={{ opacity: isPending ? 0.7 : 1 }}>
{tab === 'overview' && <Overview />}
{tab === 'lessons' && <LessonsTab />} {/* slow to render */}
{tab === 'reviews' && <Reviews />}
</div>
</>
);
}Now, if the user clicks “Lessons” (slow) and then immediately “Reviews”, React abandons the half-finished Lessons render and switches to Reviews. Without the transition, the UI would freeze until Lessons finished.
isPendingis true while the transition is in progress — use it for a subtle indicator.- During a transition, React keeps showing the old UI instead of hiding it behind a Suspense fallback.
Things to know about transitions
- Text inputs can’t be controlled by a transition. The input’s own value must update urgently. Use two pieces of state — or
useDeferredValue(below). - Transitions can be async. As you saw in Form actions, functions passed to
startTransitioncanawait; React keepsisPendingtrue until they finish. State updates after anawaitneed their ownstartTransitionwrapper. - There’s a standalone
startTransition(imported fromreact) for code outside components, withoutisPending. - Routers use transitions for navigation, which is why the old page stays visible while the next one loads.
useDeferredValue
When you don’t control the state update — for example, a value arrives as a prop — or when an input must stay urgent, use useDeferredValue:
import { memo, useDeferredValue, useState } from 'react';
function SearchPage() {
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.5 : 1, transition: 'opacity 0.2s' }}>
<SlowResults query={deferredQuery} />
</div>
</>
);
}
const SlowResults = memo(function SlowResults({ query }) {
// filters and renders thousands of items
});What happens when the user types:
- React re-renders immediately with the new
querybut the olddeferredQuery. The input updates instantly;SlowResultsgets the same prop as before, and because it’s memoised, it’s skipped. - Then React starts a background render with the new
deferredQuery. If the user types again, this render is abandoned and restarted with the latest value.
Comparing query !== deferredQuery tells you the results are stale, so you can dim them.
memomatters here. IfSlowResultsre-rendered whenever its parent did, step 1 would still be slow. With React Compiler enabled, this happens automatically.
useDeferredValue with Suspense
If SlowResults fetches data with Suspense, the deferred value also stops the results flashing back to a loading fallback on each keystroke — the old results stay visible (dimmed) until the new ones are ready.
Debouncing vs deferring
You might reach for a debounce (wait 300 ms after typing stops). The difference:
| Debounce | useDeferredValue |
|
|---|---|---|
| Delay | fixed, even on fast devices | none — renders as soon as the device can |
| Interruptible | no — once a render starts, it blocks | yes — abandoned if the user types again |
| Good for | limiting network requests | keeping rendering responsive |
They solve different problems and work well together: defer rendering, debounce fetching.
When to use which
| Situation | Tool |
|---|---|
| You trigger the update (tab click, filter change) | useTransition |
| You need a pending flag | useTransition |
| A value comes from props or from an input that must stay urgent | useDeferredValue |
| Rendering is always slow, even once | optimise the component, or virtualise the list |
Transitions don’t make work faster — they make it interruptible. If a render takes 2 seconds, it still takes 2 seconds; the difference is that the user can keep typing and clicking meanwhile.
A responsive course search
SlowResults({ query }) renders 2,000 items and is deliberately slow. Build SearchPage so that typing in the input stays instant: use useDeferredValue to pass a deferred query to the (memoised) results, and dim the results to 50% opacity while they’re stale (when query !== deferredQuery). Then add tabs (“All”, “Beginner”, “Advanced”) that switch with useTransition, showing “Loading…” next to the tabs while the transition is pending.
import { memo, useDeferredValue, useState, useTransition } from 'react';
const SlowResults = memo(function SlowResults({ query, level }) {
// provided: renders 2,000 filtered items slowly
});
export function SearchPage() {
// your code here
}import { memo, useDeferredValue, useState, useTransition } from 'react';
const SlowResults = memo(function SlowResults({ query, level }) {
// provided: renders 2,000 filtered items slowly
return <ul>{/* … */}</ul>;
});
export function SearchPage() {
const [query, setQuery] = useState('');
const [level, setLevel] = useState('all');
const deferredQuery = useDeferredValue(query);
const [isPending, startTransition] = useTransition();
const isStale = query !== deferredQuery;
return (
<>
<input aria-label="Search" value={query} onChange={(e) => setQuery(e.target.value)} />
<div role="tablist">
{['all', 'beginner', 'advanced'].map((l) => (
<button
key={l}
role="tab"
aria-selected={level === l}
onClick={() => startTransition(() => setLevel(l))}
>
{l}
</button>
))}
{isPending && <span>Loading…</span>}
</div>
<div style={{ opacity: isStale ? 0.5 : 1 }}>
<SlowResults query={deferredQuery} level={level} />
</div>
</>
);
}Summary
- Urgent updates (typing, clicking) need instant feedback; non-urgent ones (big list renders) can wait.
startTransition(() => setState(…))marks an update as non-urgent; React can interrupt it to handle typing.useTransition()adds anisPendingflag for showing that a transition is in progress.useDeferredValue(value)gives you a version of a value that lags behind during heavy renders.- The slow component must be memoised (or compiled) so it only re-renders when the deferred value changes.