Sub topic 2
Error boundaries: the idea
This chapter explains what an error boundary is and what it promises. It assumes you know React function components and JSX, props and state, throw and try/catch, and basic Promises. The next chapter shows how to write one.
The problem a boundary solves
When a component throws while rendering, React cannot finish building the UI. Without a boundary, React unmounts the whole tree, and the user sees a blank page. One broken widget, such as a profile card that reads a property of undefined, takes down the entire app.
An error boundary is a component that catches errors thrown while rendering its children. It replaces the broken subtree with a fallback UI, and the rest of the app keeps running.
The core idea
A boundary works like a try/catch for a section of the component tree. Plain try/catch cannot do this job. It only wraps imperative code that you call yourself, and rendering is done by React, not by your code. A boundary is declarative: you wrap a part of the tree, and React routes errors from that part to the boundary.
<ErrorBoundary fallback={<p>Something went wrong.</p>}>
<Profile />
</ErrorBoundary>If Profile or any component below it throws during rendering, React looks for the nearest boundary above the failing component. That boundary shows its fallback in place of its children. Siblings and ancestors of the boundary are untouched.
What counts as a boundary
In React, a component becomes an error boundary by defining one or both of these class lifecycle methods:
| Method | When it runs | Purpose |
|---|---|---|
| `static getDerivedStateFromError(error)` | During rendering, after a descendant throws | Return new state so the next render shows the fallback |
| `componentDidCatch(error, info)` | After the fallback is committed | Run side effects such as logging |
There is no function-component equivalent built into React. You either write a small class once, or use a library that provides one. Later chapters cover both. Your application components can stay as function components.
What a boundary guarantees
A boundary catches errors from these places in its children:
- The render phase of any descendant component.
- Lifecycle methods of descendant class components.
- Constructors of descendant class components.
A boundary does not catch every error in your app. Errors in event handlers, asynchronous code, and the boundary itself fall outside its scope. A later chapter covers these limits and how to handle them.
Boundaries can be nested
You can place many boundaries in one app. An error goes to the closest boundary above the component that threw. If that boundary itself fails while rendering its fallback, the error moves to the next boundary up.
<ErrorBoundary fallback={<p>The app crashed.</p>}>
<Layout>
<ErrorBoundary fallback={<p>Sidebar unavailable.</p>}>
<Sidebar />
</ErrorBoundary>
<Content />
</Layout>
</ErrorBoundary>Here a failure in Sidebar shows "Sidebar unavailable." and Content keeps working. A failure in Content is caught by the outer boundary, which replaces the whole layout.
Common mistakes
- Expecting a boundary to catch errors in its own render. A boundary protects what is below it. If the boundary's own
renderthrows, the error propagates to the next boundary up. - Wrapping nothing. A boundary only helps if it is placed above the components that might fail. Defining one and never using it has no effect.
- Treating
try/catcharound JSX as a substitute. Writingtry { return <Profile /> } catch {}does not work.Profileis not called at that point. React renders it later, outside yourtryblock.
Summary of the mental model
- A component throws during rendering.
- React walks up the tree to find the nearest boundary.
- The boundary's state changes and it renders its fallback.
- Everything outside the boundary stays mounted and interactive.
- If no boundary exists, React unmounts the whole tree.
Exercise
An app has one error boundary around the whole app and a second boundary around a Comments section. A component inside Comments throws while rendering. What does the user see, and which boundary handles it? What changes if the second boundary is removed?
Show answer
The nearest boundary above the failing component is the one around Comments, so only the comments section is replaced by its fallback. The rest of the app stays visible. If the second boundary is removed, the error reaches the outer boundary, and the fallback replaces the whole app.
Exercise
Why can't a try/catch inside a component's function body catch an error thrown by a child component that it returns as JSX?
Show answer
Returning JSX only describes the child element. It does not call the child. React renders the child later, after your function has returned, so the child's error happens outside your try block. Only an error boundary above the child can catch it.