Sub topic 5
Where to put Suspense boundaries
This chapter assumes you know function components and props, useState, ES module imports and exports, and the basics of React.lazy and Suspense from the earlier chapters. You should be able to write lazy(() => import('./Chart')) and wrap it in a <Suspense fallback={...}>. Here the question is where the <Suspense> tags go and how much of the page each one covers.
What a boundary decides
A Suspense boundary is the <Suspense> element and the subtree inside it. When any component in that subtree suspends, React stops showing that subtree and shows the boundary's fallback instead. A component suspends when it is a lazy component whose code has not loaded yet, or when it reads a promise that has not resolved (covered in a later chapter).
React looks for the nearest Suspense boundary above the suspending component. Everything inside that boundary is replaced by the fallback. Everything outside it stays as it was.
So the boundary's position controls the size of the loading region. A high boundary hides a lot of the page behind one fallback. A low boundary hides only a small piece. Placement is a user-experience decision, not a syntax detail.
If no boundary exists above a suspending component, React throws an error. In React 18 and 19 you must have at least one <Suspense> ancestor, and a common choice is one near the root.
One boundary at the top
The simplest layout puts a single boundary around the whole app.
import { lazy, Suspense } from 'react';
const Chart = lazy(() => import('./Chart'));
const Comments = lazy(() => import('./Comments'));
export default function Page() {
return (
<Suspense fallback={<p>Loading page...</p>}>
<Header />
<Chart />
<Comments />
</Suspense>
);
}This is easy to write, and it has two drawbacks.
Headerdoes not suspend, but it disappears whileChartloads, because it sits inside the same boundary.- The boundary waits for all suspended children. If
Chartarrives in 100 ms andCommentsin 900 ms, the user sees nothing until 900 ms, then both appear together.
A single boundary does make sense as a safety net at the root. It guarantees no suspension goes uncaught. It is a poor choice as the only boundary.
One boundary per component
The opposite extreme gives each lazy component its own boundary.
import { lazy, Suspense } from 'react';
const Chart = lazy(() => import('./Chart'));
const Comments = lazy(() => import('./Comments'));
export default function Page() {
return (
<>
<Header />
<Suspense fallback={<p>Loading chart...</p>}>
<Chart />
</Suspense>
<Suspense fallback={<p>Loading comments...</p>}>
<Comments />
</Suspense>
</>
);
}Now Header is always visible, and Chart and Comments each appear as soon as they are ready. The cost is visual: several spinners can appear at once, and content can pop in at different times and push other content around.
Choosing a granularity
Between those two extremes, a useful rule is to draw a boundary around each part of the UI that the user would see as one unit that can load independently.
| Layout | User sees while loading | Main risk |
|---|---|---|
| One boundary at the root | A single fallback for the whole page | Stable content disappears, and the slowest part blocks everything |
| One boundary per section | Stable shell plus a fallback per section | Several spinners, layout shifts |
| One boundary per tiny component | Many small fallbacks | A "popcorn" effect and flicker |
| Nested boundaries | Shell first, then outer content, then inner details | More structure to maintain |
Questions that help you decide:
- Should these pieces appear together? If a chart and its legend would look broken when shown one without the other, put them in the same boundary.
- Is the content below the fold or behind an interaction? A modal, a drawer, or a tab panel is a good boundary candidate, because its loading state is isolated from the rest of the page.
- Can the fallback match the final layout? A fallback with the same dimensions as the content avoids layout shift. If you cannot make it match, a smaller boundary reduces the damage.
- Does the slowest child block the fastest? If so, split them.
Nested boundaries and loading stages
Boundaries can be nested. When a component suspends, React uses the nearest boundary, so an inner boundary handles its own subtree while the outer one stays out of the way.
import { lazy, Suspense } from 'react';
const Layout = lazy(() => import('./Layout'));
const Sidebar = lazy(() => import('./Sidebar'));
const Feed = lazy(() => import('./Feed'));
export default function App() {
return (
<Suspense fallback={<p>Loading app...</p>}>
<Layout>
<Suspense fallback={<p>Loading sidebar...</p>}>
<Sidebar />
</Suspense>
<Suspense fallback={<p>Loading feed...</p>}>
<Feed />
</Suspense>
</Layout>
</Suspense>
);
}The page loads in stages:
Layoutis not ready, so the outer fallback shows.Layoutarrives and renders.SidebarandFeedare not ready, so their own fallbacks show inside the layout.- Each one replaces its fallback as soon as its code arrives.
A fallback is itself rendered outside its own boundary's children. That means a fallback must not suspend by using the same boundary. If it does, the suspension moves up to the next boundary above.
Boundaries and state
When a boundary switches to its fallback after content has already been shown, React hides the existing content but keeps its state, so a form input inside it keeps its text once the content returns. This is different from unmounting. In contrast, content that has never finished rendering has no state to keep.
Hiding content for a fallback is also the reason a late-arriving component can cause a jarring change on a screen the user is already using. The chapter on transitions covers how to avoid that. For now, remember the rule: a new boundary that suspends after the first render replaces visible content with its fallback.
A reusable boundary for a section
Teams often wrap the pattern in a small component so every section loads the same way.
import { Suspense } from 'react';
function Skeleton({ height }) {
return (
<div
aria-busy="true"
style={{ height, background: '#eee', borderRadius: 4 }}
/>
);
}
export function Section({ height = 200, children }) {
return <Suspense fallback={<Skeleton height={height} />}>{children}</Suspense>;
}import { lazy } from 'react';
import { Section } from './Section';
const Chart = lazy(() => import('./Chart'));
export default function Dashboard() {
return (
<main>
<h1>Dashboard</h1>
<Section height={320}>
<Chart />
</Section>
</main>
);
}Passing a height lets the skeleton reserve the same space the chart will fill, which prevents layout shift when the real content arrives.
Common mistakes
- Boundary inside the lazy component. A
<Suspense>placed insideChartcannot catchChart's own loading. The boundary has to be an ancestor of the lazy component, not a descendant. - Fallbacks that are too heavy. A fallback that itself needs a large download defeats the purpose. Keep fallbacks small and make them plain markup.
- Fallbacks that cause layout shift. A one-line "Loading..." that is replaced by a 400 px chart moves everything below it. Reserve the space.
- Wrapping everything separately. Ten spinners on one screen feel slower than one. Group related pieces.
- Relying on the root boundary only. One root boundary hides the whole app on every suspension, including ones triggered later by navigation.
Reading the structure
When you review code, read each <Suspense> as the answer to two questions. What disappears when something inside it loads? What stays? If the answer to the first is "more than the user expects", move the boundary down. If the answer to the second is "so many unrelated spinners that the page looks unfinished", move the boundary up.
Exercise
A page has a Header, a lazy Chart that loads in about 100 ms, and a lazy Comments list that loads in about 900 ms. All three are inside one <Suspense> boundary at the root. Describe what the user sees, then rewrite the structure so the chart can appear without waiting for the comments, and say why Header should sit outside the boundaries.
Show answer
With one boundary, the whole subtree is replaced by the fallback until both Chart and Comments are ready, so the user sees only the fallback for about 900 ms, and Header is hidden even though it never suspends. Fix it by giving each lazy component its own boundary: <Header /> followed by <Suspense fallback={<p>Loading chart...</p>}><Chart /></Suspense> and <Suspense fallback={<p>Loading comments...</p>}><Comments /></Suspense>. The chart then appears at about 100 ms, and the comments at about 900 ms. Header does not suspend, so it should stay outside any boundary that could hide it; boundaries only need to wrap the parts that can suspend, and keeping stable content outside means it stays visible during every load.
Exercise
A component Feed contains <Suspense fallback={<Spinner />}><Posts /></Suspense>, and Feed itself is lazy and rendered with no <Suspense> ancestor. What happens, and what is the fix?
Show answer
The inner boundary cannot catch Feed loading, because a boundary only catches suspensions from components inside it, and Feed is the parent of that boundary. With no <Suspense> ancestor, React throws an error when Feed suspends. The fix is to wrap <Feed /> in a <Suspense> boundary at the place it is rendered, or add a boundary near the root as a safety net. The inner boundary can stay to handle Posts separately.