Sub topic 2
Suspense: the fallback that use relies on
This chapter covers how a Suspense boundary works, since use depends on it when it reads a promise. It assumes you can write function components, pass props, use useState and useContext, and read basic promise code. You do not need to have used Suspense before.
What a Suspense boundary does
Suspense is a React component that shows a fallback while something inside it is not ready to render. You give it a fallback prop and wrap the part of the tree that may have to wait.
import { Suspense } from 'react';
function Page() {
return (
<Suspense fallback={<p>Loading...</p>}>
<Comments />
</Suspense>
);
}While Comments cannot finish rendering, React shows <p>Loading...</p>. When Comments is ready, React swaps the fallback for the real content.
The word for "cannot finish rendering yet" is suspending. A component suspends when it needs data that has not arrived. It does not return partial output, and it does not set a loading flag itself. React stops rendering that component and looks upward for the nearest Suspense boundary.
How `use` triggers Suspense
When you call use(promise) and the promise is still pending, the component suspends. When the promise resolves, React renders the component again, and use returns the resolved value.
import { Suspense, use } from 'react';
function Message({ messagePromise }) {
const message = use(messagePromise);
return <p>Here is the message: {message}</p>;
}
export function MessageContainer({ messagePromise }) {
return (
<Suspense fallback={<p>Waiting for message...</p>}>
<Message messagePromise={messagePromise} />
</Suspense>
);
}Message has no isLoading state and no useEffect. The code reads as if the value is already there. The Suspense boundary in MessageContainer handles the waiting.
The boundary must be above the component that calls use. If no boundary exists above it, the suspension goes up to the root of the app. In a client-rendered app, that can leave the screen without the fallback you intended.
The sequence of events
The steps below show what happens from the first render to the final one.
Two details matter here:
- The component function runs more than once. Any code before
useruns again on the second render, so keep that code free of side effects. - State from a suspended first render is not kept. A component that suspends before it has ever shown anything has nothing to preserve, so it starts fresh when it renders again.
Where to place a boundary
A boundary decides how much of the page is replaced by a fallback. Placement is a design choice.
- A single boundary near the top of the page shows one fallback until everything inside is ready. This is simple, but a slow part holds back the fast parts.
- Several smaller boundaries let each section appear when its own data is ready.
- Boundaries can be nested. A suspended component is handled by the nearest boundary above it.
import { Suspense } from 'react';
import { Profile } from './Profile';
import { Feed } from './Feed';
export function Dashboard({ profilePromise, feedPromise }) {
return (
<main>
<Suspense fallback={<p>Loading profile...</p>}>
<Profile profilePromise={profilePromise} />
</Suspense>
<Suspense fallback={<p>Loading feed...</p>}>
<Feed feedPromise={feedPromise} />
</Suspense>
</main>
);
}Here Profile and Feed wait independently. If the feed is slow, the profile still appears as soon as it is ready.
Fallbacks for content that is already visible
A fallback also appears the first time a boundary needs to show something. After content has been shown, React treats later updates differently. If an update causes already visible content to suspend again, React normally keeps showing the old content only when the update is marked as a transition, for example with startTransition. Without a transition, the boundary goes back to its fallback.
import { startTransition, useState } from 'react';
export function Tabs({ renderTab }) {
const [tab, setTab] = useState('home');
function select(next) {
startTransition(() => {
setTab(next);
});
}
return (
<div>
<button onClick={() => select('home')}>Home</button>
<button onClick={() => select('posts')}>Posts</button>
{renderTab(tab)}
</div>
);
}Transitions are a separate topic, so this chapter only notes that they exist. What matters now is that the fallback you see is a rule about what to show when there is nothing ready, and not a loading indicator that always appears.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| No Suspense boundary above the component that calls `use` | The suspension reaches the root, so there is no local fallback | Wrap the component in `Suspense` with a `fallback` |
| One boundary around the whole page | A slow section hides everything | Add boundaries around independent sections |
| A fallback that is much taller or shorter than the content | The layout jumps when content arrives | Give the fallback a similar size |
| Side effects before the `use` call | They run again on each render attempt | Move them out of render |
Exercises
Exercise
A component calls use on a pending promise and has a Suspense boundary above it. What does React show until the promise resolves, and what does the component do when the promise resolves?
Show answer
React shows the boundary's fallback. When the promise resolves, React renders the component again, use returns the resolved value, and the component's real content replaces the fallback.
Exercise
A page has a profile section that loads in 100 ms and a feed that loads in 3 seconds, both wrapped in a single Suspense boundary. How would you change the layout so the profile appears as soon as it is ready?
Show answer
Give each section its own Suspense boundary, so the profile is handled by one boundary and the feed by another. The profile then shows when its data is ready, while the feed's fallback stays in place.