Sub topic 5
Where to put Suspense boundaries
This chapter assumes you can write function components, know how React.lazy loads a component from a dynamic import(), and have seen a basic <Suspense fallback> wrapper. Here you decide where those wrappers go.
What a boundary controls
A Suspense boundary catches any component below it that suspends, meaning it is not ready to render yet. React then shows the boundary's fallback in place of everything inside that boundary. When the suspended component is ready, React swaps the real content back in.
The key rule is that a component suspends to the nearest Suspense boundary above it. If there is no boundary, React throws an error for the lazy component. So the placement of boundaries decides two things:
- Which part of the screen is replaced by a fallback.
- Which parts keep showing while something else loads.
One boundary at the top
The simplest layout puts one boundary around the whole app.
import { lazy, Suspense } from 'react';
const Sidebar = lazy(() => import('./Sidebar'));
const Feed = lazy(() => import('./Feed'));
export default function App() {
return (
<Suspense fallback={<p>Loading...</p>}>
<Sidebar />
<Feed />
</Suspense>
);
}This works, but it has a cost. If Feed is slow, the user sees only "Loading..." even after Sidebar is ready, because the single boundary hides both. Any suspended child blanks the whole area.
Boundaries close to the slow part
A narrower boundary limits the loading state to the piece that needs it.
import { lazy, Suspense } from 'react';
const Sidebar = lazy(() => import('./Sidebar'));
const Feed = lazy(() => import('./Feed'));
export default function App() {
return (
<div className="layout">
<Suspense fallback={<p>Loading menu...</p>}>
<Sidebar />
</Suspense>
<Suspense fallback={<p>Loading feed...</p>}>
<Feed />
</Suspense>
</div>
);
}Each section now loads on its own. The sidebar can appear while the feed is still downloading.
Nesting boundaries
Boundaries can be nested. The inner boundary handles its own children, and the outer one handles everything else.
import { lazy, Suspense } from 'react';
const Page = lazy(() => import('./Page'));
const Comments = lazy(() => import('./Comments'));
export default function Article() {
return (
<Suspense fallback={<p>Loading page...</p>}>
<Page>
<Suspense fallback={<p>Loading comments...</p>}>
<Comments />
</Suspense>
</Page>
</Suspense>
);
}If Page is not ready, the outer fallback shows. Once Page renders, the user sees its content right away, and Comments shows its own smaller fallback until it arrives. Important content appears first and secondary content fills in later.
Choosing a granularity
More boundaries are not always better. Use these questions to decide.
- Which parts are useful on their own? Give each independent region, such as a sidebar, a chart, or a comment list, its own boundary.
- Which parts must appear together? Wrap them in one boundary so the user does not see half a layout.
- Does the fallback keep the layout stable? A fallback close to the final size avoids content jumping when the real component appears.
| Approach | Benefit | Risk |
|---|---|---|
| One boundary at the top | Simple, one loading state | Fast parts wait for slow parts |
| Boundary per region | Content appears as it is ready | Several spinners can show at once |
| Nested boundaries | Main content first, extras later | More fallbacks to design |
Common mistakes
- Putting the boundary inside the lazy component. A component cannot catch its own suspension. The boundary must be an ancestor, so place it in the parent.
- Too many tiny spinners. If every small widget has its own boundary, the page flickers with loading indicators. Group items that load at about the same time.
- Fallbacks that change the layout. A one-line "Loading..." replaced by a tall chart pushes the page around. Use a placeholder with a similar height.
Exercise
A dashboard has a header, a lazy Chart, and a lazy Table, all inside one Suspense boundary. The Table takes much longer to load than the Chart. What does the user see, and how would you change the code so the Chart appears as soon as it is ready?
Show answer
The user sees only the single fallback until both Chart and Table are ready, because the one boundary replaces everything inside it. Give Chart and Table their own Suspense boundaries (siblings), each with its own fallback. The Chart then appears when its chunk loads, and the Table shows its fallback until it arrives.