Sub topic 8
Keeping the UI stable with transitions
This chapter assumes you know function components, useState, and the basics of React.lazy and Suspense from the earlier chapters. You should also be comfortable with promises and ES module imports.
The problem: a fallback replaces visible content
A Suspense boundary shows its fallback whenever something inside it suspends. That includes the first render of a lazy component. It also includes a state update that makes an already-mounted boundary render a lazy component whose chunk has not loaded yet.
Consider a tabbed page. The user sees the Profile tab and clicks Settings. The Settings chunk has not loaded, so the boundary suspends. By default React hides the Profile content and shows "Loading...". On a fast connection this appears as a brief flash. On a slow one, the user loses content they were reading and gets a spinner in return.
What a transition does
A transition marks a state update as non-urgent. When an update inside a transition would suspend, React does not show the fallback for a boundary that is already visible. It keeps the old UI on screen, renders the new UI in the background, and commits the change when the chunk is ready.
You create a transition with the useTransition hook. It returns two values:
isPending, a boolean that istruewhile the transition is in progress.startTransition, a function that takes a callback. State updates called inside it are treated as the transition.
A tabbed example
This component wraps the tab change in a transition. The current tab stays visible while the next chunk loads, and isPending dims it.
import { lazy, Suspense, useState, useTransition } from 'react';
const Profile = lazy(() => import('./Profile'));
const Settings = lazy(() => import('./Settings'));
export default function Tabs() {
const [tab, setTab] = useState('profile');
const [isPending, startTransition] = useTransition();
function select(next) {
startTransition(() => setTab(next));
}
return (
<div>
<button onClick={() => select('profile')}>Profile</button>
<button onClick={() => select('settings')}>Settings</button>
<div style={{ opacity: isPending ? 0.6 : 1 }}>
<Suspense fallback={<p>Loading...</p>}>
{tab === 'profile' ? <Profile /> : <Settings />}
</Suspense>
</div>
</div>
);
}Follow what happens when the user clicks Settings:
startTransitionrunssetTab('settings')as a transition.- React starts rendering the Settings branch and finds that
Settingshas not loaded. It begins the download. - Because this is a transition, React leaves the Profile tab on screen.
isPendingbecomestrue, and the wrapper dims. - When the chunk arrives, React finishes rendering and commits the Settings tab.
isPendingreturns tofalse.
The Suspense fallback still appears on the very first render, because there is no old content to keep. A transition only protects content that is already visible.
Rules and limits
- The state update must be inside the callback you pass to
startTransition. Updates outside it are urgent and show the fallback. - The callback runs synchronously. Do not put
awaitcalls before the state update and expect it to remain part of the transition. In React 18, updates after anawaitare not marked as transitions. - A transition only keeps content for a boundary that is already showing. A boundary that is newly mounted by the update still shows its fallback.
- Use
isPendingfor feedback such as dimming, a small spinner, or disabling a button. Without feedback, a slow chunk makes the click look ignored. - Do not use transitions for controlled text inputs. The input must update immediately.
Transitions and error boundaries
A transition does not change how failures work. If the chunk request fails, the lazy component rejects, and the error propagates to the nearest error boundary. Keep the error boundary outside the Suspense boundary, as in the previous chapter, so a failed tab load still shows a recovery message.
Summary
A plain state update that suspends replaces visible content with a fallback. Wrapping the update in startTransition keeps the old content, and isPending lets you show that work is happening. The fallback remains the right choice for first loads.
Exercise
A user clicks a tab, and the tab content is replaced by "Loading..." for a moment, even though you already wrapped the Suspense boundary around the tab content. The click handler calls setTab(next) directly. What change keeps the old tab visible while the new chunk loads, and how can you tell the user something is happening?
Show answer
Call useTransition and run the update as startTransition(() => setTab(next)). React then keeps the current tab on screen until the new chunk is ready. Use the isPending value from the hook to dim the content, show a small spinner, or disable the buttons while the transition runs.
Exercise
Does a transition remove the fallback the first time a lazy component renders on a page? Explain why or why not.
Show answer
No. A transition only keeps content that is already visible. On the first render there is no previous UI to keep, so the Suspense boundary still shows its fallback until the chunk loads.