Sub topic 7
Where to place boundaries
This chapter assumes you can write a class-based error boundary with getDerivedStateFromError and a fallback prop, and that you know which errors boundaries do not catch. The examples reuse the ErrorBoundary component from earlier in this tutorial.
The core rule
A boundary catches errors from components below it in the tree. When one is caught, React replaces that boundary's whole subtree with the fallback. Placement therefore decides how much of the screen disappears when something fails.
- A boundary placed high in the tree is simple, but one failure blanks a large area.
- A boundary placed low isolates failures, but each one needs its own fallback, and many small fallbacks can clutter the page.
Most apps need a few layers, not one boundary or dozens.
A layered layout
Start with one boundary around the whole app as a last resort. Then add boundaries around regions that load their own data or run independent features.
function App() {
return (
<ErrorBoundary fallback={<p>The app hit a problem. Please reload.</p>}>
<Header />
<div className="layout">
<ErrorBoundary fallback={<p>Sidebar unavailable.</p>}>
<Sidebar />
</ErrorBoundary>
<main>
<ErrorBoundary fallback={<p>Could not load the feed.</p>}>
<Feed />
</ErrorBoundary>
</main>
</div>
</ErrorBoundary>
);
}If Sidebar throws, Header and Feed keep working. If Header throws, only the outer boundary can catch it, so the whole app falls back.
Choosing a level
Ask these questions about each part of the page.
- Can the page still be useful without this part? If so, it is a candidate for its own boundary.
- Does this part depend on data or code that the rest of the page does not? Separate failure sources should get separate boundaries.
- Would a fallback here make sense to a user? A boundary around a single icon usually produces a fallback that looks worse than the failure.
Common placement points:
| Level | Typical target | What the user sees on failure |
|---|---|---|
| App root | The whole tree | A full-page message with a reload option |
| Route or page | One route's content | The navigation stays, the page body shows a message |
| Widget or region | A chart, feed, or sidebar | One panel shows a message, the rest works |
| List item | One row in a list | A single broken row, other rows render |
Per-item boundaries in lists
When one bad record could break a whole list, wrap each item.
function CommentList({ comments }) {
return (
<ul>
{comments.map((comment) => (
<li key={comment.id}>
<ErrorBoundary fallback={<span>This comment could not be shown.</span>}>
<Comment data={comment} />
</ErrorBoundary>
</li>
))}
</ul>
);
}Use this where items come from untrusted or inconsistent data. For a list of ten reliable rows it adds noise without benefit.
Common mistakes
- Putting the boundary inside the component that throws. A boundary cannot catch its own errors, or those of the component that renders it. It must sit above. Wrapping the JSX returned by
Feeddoes nothing ifFeeditself throws before returning. - Relying only on a root boundary. Every failure then looks the same and wipes the page, including navigation the user needs to leave the broken area.
- Sharing one boundary across unrelated siblings. If two widgets sit inside one boundary, a failure in either hides both.
- Vague fallbacks. "Something went wrong" in every layer tells the user nothing. Name what failed, such as "Could not load the feed."
Boundaries and state
When a boundary shows its fallback, React unmounts the subtree it replaced, so state inside that subtree is lost. A higher boundary loses more state than a lower one. If a form holds long user input, put a boundary below it or keep the draft in a parent outside the failing region. Letting users retry is covered in a later chapter.
Exercise
A dashboard has a header, a sales chart, and a table of orders. The chart sometimes throws when the API returns unexpected data. You have one boundary wrapping the entire dashboard. What change keeps the header and table visible when the chart fails, and why does wrapping the JSX inside the Chart component not work?
Show answer
Wrap the chart alone in its own ErrorBoundary with a fallback such as "The chart could not be shown," placed in the dashboard component around the Chart element. The header and table sit outside that boundary, so they keep rendering. Putting a boundary inside Chart's returned JSX does not work because a boundary only catches errors thrown by components below it. An error thrown in Chart's own render code happens above that inner boundary.