Sub topic 5
What error boundaries do not catch
This chapter assumes you can write React function components, know how throw and try/catch work, and have read the previous chapters on writing a class-based boundary. A boundary has a narrow job. Knowing its limits tells you where you still need other error handling.
Why the limit exists
React uses a boundary only for errors thrown while it is rendering. If a component throws during render, React can't produce a valid UI for that subtree, so it looks for the nearest boundary and shows the fallback instead.
An error in an event handler is different. The handler runs after rendering has finished, and the UI on screen is still valid. React has no broken output to replace, so it doesn't involve a boundary.
The cases a boundary skips
| Where the error happens | Caught by a boundary? | What to use |
|---|---|---|
| Rendering, lifecycle methods, constructors of children | Yes | The boundary |
| Event handlers such as `onClick` | No | `try/catch` in the handler |
| Asynchronous code (`setTimeout`, promises, code after `await`) | No | `try/catch` or `.catch()` |
| Server-side rendering | No | Error handling on the server |
| The boundary component itself | No | A boundary higher in the tree |
Event handlers
An error thrown in a handler is an ordinary JavaScript error. It appears in the console, but the component stays on screen.
function DeleteButton({ onDelete }) {
function handleClick() {
// If onDelete throws, no boundary sees it.
onDelete();
}
return <button onClick={handleClick}>Delete</button>;
}Asynchronous code
By the time a timer fires or a promise settles, React's render has already returned. An error thrown after an await rejects a promise, and no boundary sees it. The fix is a try/catch that keeps the failure local.
function SaveButton({ save }) {
const [message, setMessage] = useState("");
async function handleClick() {
try {
await save();
setMessage("Saved.");
} catch (error) {
console.error(error);
setMessage("Could not save. Please try again.");
}
}
return (
<div>
<button onClick={handleClick}>Save</button>
<p role="status">{message}</p>
</div>
);
}Remember to import useState from "react" when you run this. The inline message is often the better design for a failed save anyway, because the rest of the form stays usable.
Server-side rendering
Boundaries work in the browser. When a component throws during server rendering, the server's rendering API reports the error, and your server code has to handle it. That might mean sending an error page or falling back to client-side rendering.
Errors in the boundary itself
A boundary catches errors from its children, not from its own render. If the fallback throws, the error moves up to the next boundary above. If none exists, React unmounts the whole tree, which is the behaviour you were trying to avoid.
// If this fallback throws, this boundary cannot catch it.
<ErrorBoundary fallback={<Broken />}>
<Profile />
</ErrorBoundary>Keep fallbacks simple. Avoid data fetching and complex logic in them.
A quick way to decide
Common mistakes
- Wrapping a button in a boundary and expecting it to catch a failed click handler.
- Putting a
try/catcharound<Profile />in JSX. Atry/catchcan't catch errors from rendering a child, because React renders it later. - Assuming that an
asyncfunction used inuseEffectreports its errors to a boundary. It doesn't unless you route the error there yourself.
The next chapter shows how to send handler and async errors into a boundary when you want a boundary-level fallback.
Exercise
A component calls fetch inside a useEffect, and the request rejects. The component is wrapped in an error boundary. Does the boundary show its fallback? Why or why not, and what should you do?
Show answer
No. The rejection happens asynchronously, after rendering has finished, so the boundary never sees it. Wrap the request in try/catch (or add .catch()), and store the error in state to show an inline message. If you want the boundary's fallback, pass the error on to the boundary explicitly.
Exercise
A boundary's fallback UI reads user.name, and user is undefined. What happens?
Show answer
The fallback throws while rendering. A boundary can't catch its own errors, so the error goes to the next boundary above it. If there is none, React unmounts the whole tree.