React Transitions
Not all updates are equally urgent. When you type into a search box, the input must update instantly, but the result list can lag a little. React lets you mark updates as non-urgent — transitions — so it can keep the UI responsive, interrupt stale work, and avoid flashing loading spinners.
| API | What it does |
|---|---|
useTransition() |
returns [isPending, startTransition]; updates inside startTransition are non-urgent |
startTransition(fn) |
same, without the pending flag (import from react) |
useDeferredValue(value) |
returns a "lagging" copy of a value that updates as a transition |
The problem: a slow render blocks typing
Each item below is artificially slow to render. Without transitions, every keystroke must wait for the whole list:
import { memo, useDeferredValue, useState } from 'react';
function SlowItem({ text }) {
const start = performance.now();
while (performance.now() - start < 1) {} // 1 ms per item
return <li>{text}</li>;
}
const SlowList = memo(function SlowList({ query }) {
const items = [];
for (let i = 0; i < 250; i++) items.push(<SlowItem key={i} text={`${query || 'item'} #${i + 1}`} />);
return <ul style={{ height: 150, overflow: 'auto' }}>{items}</ul>;
});
export default function App() {
const [text, setText] = useState('');
const [useDeferred, setUseDeferred] = useState(true);
const deferredText = useDeferredValue(text);
const listQuery = useDeferred ? deferredText : text;
const stale = text !== listQuery;
return (
<div>
<label><input type="checkbox" checked={useDeferred} onChange={(e) => setUseDeferred(e.target.checked)} /> use useDeferredValue</label>
<p><input value={text} onChange={(e) => setText(e.target.value)} placeholder="Type quickly…" /></p>
<div style={{ opacity: stale ? 0.5 : 1, transition: 'opacity .2s' }}>
<SlowList query={listQuery} />
</div>
</div>
);
}Uncheck the box and type fast: the input stutters. With useDeferredValue, the input stays smooth. React first re-renders with the old deferred value (cheap, because SlowList is memoized), then renders the new list in the background — and throws that work away if you type again.
Note:
useDeferredValueonly helps if the slow component can skip the urgent render, so wrap it inmemo(or let the React Compiler do it).
useTransition for state updates
When you own the state update, wrap it in startTransition. isPending tells you a transition is in progress, so you can show a subtle hint instead of a blank screen:
import { memo, useState, useTransition } from 'react';
function heavy(ms) {
const start = performance.now();
while (performance.now() - start < ms) {}
}
function SlowPost({ n }) {
heavy(2); // 200 posts × 2 ms ≈ 400 ms in total
return <li>Post #{n}</li>;
}
const TABS = {
about: () => <p>👋 About: a fast tab.</p>,
posts: () => {
const posts = [];
for (let i = 1; i <= 200; i++) posts.push(<SlowPost key={i} n={i} />);
return <ul style={{ height: 120, overflow: 'auto' }}>{posts}</ul>;
},
contact: () => <p>📧 Contact: hello@example.com</p>,
};
const TabPanel = memo(function TabPanel({ tab }) {
const Panel = TABS[tab];
return <Panel />;
});
export default function App() {
const [tab, setTab] = useState('about');
const [useTx, setUseTx] = useState(true);
const [isPending, startTransition] = useTransition();
function select(next) {
if (useTx) startTransition(() => setTab(next));
else setTab(next);
}
return (
<div>
<label><input type="checkbox" checked={useTx} onChange={(e) => setUseTx(e.target.checked)} /> use startTransition</label>
<p>
{Object.keys(TABS).map((t) => (
<button key={t} onClick={() => select(t)} style={{ marginRight: 4, fontWeight: t === tab ? 'bold' : 'normal' }}>{t}</button>
))}
{isPending && <span> ⏳</span>}
</p>
<div style={{ opacity: isPending ? 0.6 : 1 }}>
<TabPanel tab={tab} />
</div>
</div>
);
}With transitions on, click posts and then immediately contact: React abandons the half-finished posts render and shows contact right away. Without transitions the click on contact has to wait until the posts tab is fully rendered. React can only pause between components, which is why the slowness is spread over many small SlowPost components here.
Transitions and Suspense
Transitions also change how <Suspense> behaves. Normally, when content suspends again, React replaces it with the fallback. Inside a transition React instead keeps showing the old content (with isPending = true) until the new content is ready, avoiding a jarring flash:
import { Suspense, use, useState, useTransition } from 'react';
const wait = (ms, v) => new Promise((r) => setTimeout(() => r(v), ms));
const loadPage = (n) => wait(900, [`Article ${n * 3 - 2}`, `Article ${n * 3 - 1}`, `Article ${n * 3}`]);
function Page({ promise }) {
const items = use(promise);
return <ul>{items.map((i) => <li key={i}>{i}</li>)}</ul>;
}
export default function App() {
const [page, setPage] = useState(1);
const [promise, setPromise] = useState(() => loadPage(1));
const [isPending, startTransition] = useTransition();
const [smooth, setSmooth] = useState(true);
function go(n) {
const update = () => { setPage(n); setPromise(loadPage(n)); };
smooth ? startTransition(update) : update();
}
return (
<div>
<label><input type="checkbox" checked={smooth} onChange={(e) => setSmooth(e.target.checked)} /> navigate in a transition</label>
<p>
<button onClick={() => go(page - 1)} disabled={page === 1}>◀ Prev</button> Page {page}{' '}
<button onClick={() => go(page + 1)}>Next ▶</button> {isPending && '⏳'}
</p>
<div style={{ opacity: isPending ? 0.5 : 1 }}>
<Suspense fallback={<p>Loading page…</p>}>
<Page promise={promise} />
</Suspense>
</div>
</div>
);
}Routers use exactly this technique for navigation: the old page stays visible and dimmed while the next one loads.
Rules and tips
- Only state updates can be transitions — not updates to controlled input values. Keep
setTexturgent and defer the expensive consumer instead. - In React 19, the function passed to
startTransitionmay be async; React 19 calls these Actions (next lessons). - Transitions don't make rendering faster. They make it interruptible and keep the interface responsive.
| Use | When |
|---|---|
useDeferredValue |
you receive a value (prop or state) and a slow part depends on it |
useTransition |
you trigger the state update yourself and want isPending |
Test yourself
What does isPending from useTransition indicate?
- An error occurred
- A transition update is still being rendered
- The component is unmounted
- A promise was rejected
Why should you NOT wrap a controlled input's setText in startTransition?
- It causes an error
- Typing must update urgently or the input feels broken
- Transitions only work with numbers
- It disables the input
During a transition, a Suspense boundary that suspends again will…
- Show its fallback immediately
- Keep showing the previous content until the new content is ready
- Throw an error
- Skip rendering forever