Sub topic 3
Keeping React Suspense UI Stable
Assume you know function components, hooks, ES modules, promises, and a bundler such as Vite or webpack. This chapter focuses on transitions, preloading, Suspense-enabled data, server rendering, and measurement.
Keeping the UI stable with transitions
A transition marks a state update as non-urgent. React can keep already visible content on screen while it renders the next lazy component. Without a transition, switching tabs may replace useful content with a Suspense fallback.
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(nextTab) {
startTransition(() => setTab(nextTab));
}
return (
<>
<button onClick={() => select('profile')}>Profile</button>
<button onClick={() => select('settings')}>Settings</button>
{isPending && <span> Updating…</span>}
<Suspense fallback={<p>Loading tab...</p>}>
{tab === 'profile' ? <Profile /> : <Settings />}
</Suspense>
</>
);
}isPending lets you show progress without hiding the current tab. Use transitions for navigation or tab changes, not for urgent updates such as typing into a text field.
Preloading components before they are needed
A shared dynamic import function can start downloading a chunk on hover or keyboard focus. The bundler usually reuses the cached module when lazy later renders it.
const loadSettings = () => import('./Settings');
const Settings = lazy(loadSettings);
<button onMouseEnter={loadSettings} onFocus={loadSettings}>
Settings
</button>Suspense for data fetching and `use`
In React 19, use(promise) reads a promise passed from a parent. If the promise is pending, the nearest Suspense boundary renders its fallback.
import { Suspense, use } from 'react';
function Comments({ commentsPromise }) {
const comments = use(commentsPromise);
return <ul>{comments.map(c => <li key={c.id}>{c.text}</li>)}</ul>;
}
export function Post({ commentsPromise }) {
return (
<Suspense fallback={<p>Loading comments...</p>}>
<Comments commentsPromise={commentsPromise} />
</Suspense>
);
}The framework or data library must create and cache promises in a Suspense-compatible way. Do not create a new promise during every render.
Suspense on the server
With server rendering, Suspense can stream completed page sections as they become ready. The browser can also selectively hydrate those sections, attaching event handlers to important interactive content before less important content.
Common pitfalls and measuring the result
Avoid boundaries that cover the whole application, because one slow chunk can hide the entire page. Avoid tiny boundaries that create many distracting spinners. Add an error boundary around lazy content to handle failed chunk requests.
Measure before and after splitting. Compare initial JavaScript bytes, chunk download time, time to interactive, and real-user navigation delays. A smaller bundle is useful only if it improves these user-visible measurements.
Exercise
Why wrap a tab change in startTransition?
Show answer
It lets React keep the current content visible while the next Suspense-controlled content loads.