Sub topic 9
Transitions, deferred values and Suspense
This chapter assumes you know useTransition, useDeferredValue, React.memo, and the basic idea of a <Suspense> boundary with a fallback. It also assumes you can read a component that suspends while its data loads.
The problem: fallbacks hide content
When a component suspends, React shows the fallback of the nearest <Suspense> boundary. If the user clicks a link and the new page suspends, an urgent update replaces the page they were reading with a spinner. That is often worse than waiting a moment on the old page.
Transitions and deferred values change this behavior. React treats the suspended render as non-urgent, so it keeps the current UI on screen until the new UI is ready.
Transitions with Suspense
Wrap the state update in a transition. In this example, Artist is any component that suspends while it loads data. It could use use() with a cached promise or a framework's data API.
import { Suspense, useState, useTransition } from 'react';
function App() {
const [artistId, setArtistId] = useState('beatles');
const [isPending, startTransition] = useTransition();
function open(id) {
startTransition(() => {
setArtistId(id);
});
}
return (
<>
<button onClick={() => open('beatles')}>Beatles</button>
<button onClick={() => open('stones')}>Stones</button>
<div style={{ opacity: isPending ? 0.6 : 1 }}>
<Suspense fallback={<p>Loading...</p>}>
<Artist id={artistId} />
</Suspense>
</div>
</>
);
}While the new Artist suspends, React does not commit the new tree. The old artist stays visible, and isPending is true, so you can dim it. When the data arrives, React commits the new tree in one step.
Deferred values with Suspense
useDeferredValue gives the same protection when you cannot wrap the setState call, for example when the value arrives as a prop.
import { Suspense, useState, useDeferredValue } 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)} />
<Suspense fallback={<p>Loading...</p>}>
<div style={{ opacity: isStale ? 0.6 : 1 }}>
<SearchResults query={deferredQuery} />
</div>
</Suspense>
</>
);
}SearchResults here suspends while it loads data. React first re-renders with the old deferredQuery. It then tries a background render with the new value. If that render suspends, React discards it and keeps showing the old results. The input stays responsive throughout.
What still shows a fallback
A transition only protects content that is already revealed. Two cases still show a fallback:
- A
<Suspense>boundary that is newly mounted during the transition shows its fallback. For example, navigating to a page that has its own boundary shows that boundary's fallback. - The first render of a boundary on initial load shows its fallback. Nothing is on screen yet to preserve.
To control this, place boundaries deliberately. A boundary above the changing content keeps the old UI during a transition. A boundary inside the new content shows a placeholder for that part.
Common errors
- Putting the
<Suspense>boundary inside the component that suspends. The boundary must be an ancestor of the suspending component. - Updating state after an
awaitwithout a newstartTransition. That update is urgent and can trigger a fallback. - Expecting
isPendingfrom oneuseTransitionto track transitions started by a different hook. - Forgetting
memoon a child that receives the deferred value. Without it, the child re-renders with the urgent value too, and the deferral gains nothing.
Exercise
A user clicks a tab, and the new tab's content suspends. The tab switch is not wrapped in a transition, and the screen flashes a spinner where the old tab was. What is the smallest change that keeps the old tab visible until the new one is ready, and what can you use to signal that loading is happening?
Show answer
Wrap the setTab call in startTransition from useTransition. React then keeps the old tab on screen while the new one suspends. Use the returned isPending flag to dim the old content or show a small indicator.
Exercise
A transition navigates to a page that contains its own <Suspense fallback={<Skeleton />}> boundary around slow content. Does the user see <Skeleton />?
Show answer
Yes. The boundary is newly mounted during the transition, so it shows its fallback. Only boundaries that were already revealed keep their old content.