Sub topic 5
What error boundaries do not catch
This chapter assumes you can write function components with state and effects. It also assumes you can write a class-based error boundary with getDerivedStateFromError and componentDidCatch, as in the earlier chapters, and that you are comfortable with try/catch and async/await.
An error boundary is not a general-purpose try/catch for a React app. It catches one kind of failure: errors thrown while React is working on the component tree. Errors from anywhere else pass straight through it. If you assume a boundary protects you from everything, you will ship screens that go blank or fail silently. This chapter maps the edges of that protection.
The rule: boundaries catch errors React runs
React calls your code at specific moments. It calls components to render them, and it calls lifecycle methods and effects when it commits changes to the DOM. If any of that code throws, React is in charge of the call stack and can route the error to the nearest boundary above.
Other code is not run by React. The browser runs event listeners, timers, and promise callbacks. By the time they execute, React is no longer on the call stack, so there is nothing for it to intercept.
What is caught, and what is not
| Where the error is thrown | Caught by a boundary? |
|---|---|
| Component function body while rendering | Yes |
| Constructors and lifecycle methods of children | Yes |
| `useEffect` and `useLayoutEffect` callbacks (synchronous throws) | Yes |
| Event handlers such as `onClick` | No |
| `setTimeout`, `setInterval`, `requestAnimationFrame` callbacks | No |
| Promise rejections and code after `await` | No |
| Server-side rendering | No |
| The boundary component itself | No, goes to the next boundary above |
Most readers are surprised by the row about effects. An effect is run by React during the commit phase, so a synchronous throw inside it is routed to a boundary. The part of an effect that runs after an await is a different matter. That code runs later from a promise callback, so it falls into the "not caught" group.
Event handlers
React does not call event handlers while rendering. It calls them in response to a user action, and they do not affect what the render output is. If a handler throws, React does not need to discard the tree to stay consistent, so it does nothing special. The error reaches the browser console and, in React 19, is also reported through window.reportError.
import { Component, useState } from "react";
class ErrorBoundary extends Component {
state = { error: null };
static getDerivedStateFromError(error) {
return { error };
}
render() {
if (this.state.error) {
return <p role="alert">Boundary caught: {this.state.error.message}</p>;
}
return this.props.children;
}
}
function Demo() {
const [crash, setCrash] = useState(false);
if (crash) {
throw new Error("Thrown during render");
}
function handleClick() {
throw new Error("Thrown in an event handler");
}
return (
<div>
<button onClick={handleClick}>Throw in handler (not caught)</button>
<button onClick={() => setCrash(true)}>Throw in render (caught)</button>
</div>
);
}
export default function App() {
return (
<ErrorBoundary>
<Demo />
</ErrorBoundary>
);
}Click the first button. The page still shows both buttons and the error appears only in the console. Click the second button. The state change causes a render that throws, and the boundary replaces Demo with its message.
The second button shows an indirect way to get a handler's failure into a boundary: store the error in state and throw it during the next render. The next chapter develops this idea and the library helper that wraps it. For now, remember that the throw must happen during rendering, not in the handler.
Asynchronous code
Asynchronous code is the same case with a longer delay. When handleClick awaits a fetch and the request fails, the exception is thrown in a later turn of the event loop. React's call stack is gone by then.
import { useEffect } from "react";
function Loader() {
useEffect(() => {
// Caught: this throw happens synchronously inside React's effect call.
// throw new Error("sync effect failure");
// Not caught: this runs after React has returned control to the browser.
setTimeout(() => {
throw new Error("Thrown in a timer");
}, 0);
// Not caught: an unhandled rejection is not an exception on React's stack.
Promise.reject(new Error("Rejected promise"));
}, []);
return <p>Loading…</p>;
}Timer errors become uncaught exceptions on window. A rejected promise with no handler becomes an unhandledrejection event. In neither case does the UI change, so the user sees a spinner that never finishes. This is a common production failure: the data call fails, nothing is rendered, and no fallback appears.
One related case does work. In React 19, if you pass a promise to the use hook during rendering and the promise rejects, React throws the rejection during render. A boundary above then catches it. The difference is that use reads the promise during rendering, so the failure becomes a render error.
Server-side rendering
Error boundaries are a client-side feature. When you render to a string or a stream on the server, a boundary does not catch the error and does not produce its fallback. The server renderer reports the failure through its own error callbacks, such as the onError option of the streaming APIs. What users see then depends on the API you use. With streaming and Suspense, React can send the Suspense fallback and retry the content on the client. After hydration, boundaries work normally in the browser.
Two practical consequences follow:
- Log server render errors in the server's
onErroroption. Your client boundary'scomponentDidCatchnever sees them. - A component that throws only on the server, for example because it reads
window, can break the page before any boundary exists.
The boundary itself
A boundary cannot catch an error thrown from its own render, constructor, or lifecycle methods. If it could, a bug in the fallback would cause an infinite loop: the fallback throws, the boundary catches it, and it renders the fallback again. React avoids the loop by propagating the error to the next boundary above.
function BrokenFallback() {
throw new Error("The fallback itself is broken");
}
function App() {
return (
<ErrorBoundary>
{/* Outer boundary: catches errors from the inner boundary's own render */}
<ErrorBoundary>
<Child />
</ErrorBoundary>
</ErrorBoundary>
);
}If an inner boundary's fallback throws, the outer boundary handles it. If there is no outer boundary, the error is uncaught and React unmounts the whole tree. Keep fallbacks simple. A fallback should be static markup with no data fetching and no optional chaining on values that might be missing. A fallback that depends on the same data that failed will fail in the same way.
Errors in the dev overlay
In development you will often see a full-screen error overlay even when a boundary caught the error. Tools such as Vite and Next.js install a listener that shows every error, and React also rethrows caught errors in development so they appear in the console. Dismiss the overlay with its close button or Escape key to see your fallback underneath. In a production build, caught errors do not produce the overlay. Test the fallback with a production build before you trust it.
No boundary at all
If an error is thrown during rendering and no boundary exists above it, React unmounts the whole tree. The user sees a blank page. For this reason, put at least one boundary at the root of your app, even if its fallback is only a generic "something went wrong" message with a reload button. In React 19, the onUncaughtError option of createRoot is called for errors that no boundary catches. A later chapter covers it.
A quick checklist
When a failure is not reaching your boundary, ask these questions in order:
- Was the error thrown while React was rendering, or later from a browser callback?
- Is the boundary above the component that threw, not inside it or beside it?
- Did the error come from the boundary's own render or fallback?
- Does the code run only on the server?
- Is it caught but hidden by the development overlay?
Most cases are answered by the first question.
Exercise
A component calls fetch("/api/user") inside useEffect, and the call is followed by .then(res => res.json()).then(setUser). The server returns a network error, the promise rejects, and the user sees a spinner forever, even though the component is wrapped in an error boundary. Why did the boundary not catch it, and what are two ways to fix it?
Show answer
The rejection happens in a promise callback after the effect function has returned, so React is not on the call stack and cannot route the error to a boundary. The fix is to handle the failure yourself. One way is to add a .catch or a try/catch around await, then set an error state in the component. The other way is to store the caught error in state and throw it during the next render, so that the nearest boundary catches it. The second approach is useful when you want the boundary's fallback instead of an inline message.