
What Error Boundaries Don't Catch: Event Handlers, Async Code and More
- كاتب موثّق
- React 19
محتويات المقال
ErrorBoundary with a friendly "Something went wrong, try again" fallback, and the team considers it crash-proof. Then the payment API starts rejecting some requests. Customers click "Pay", the button switches to "Paying…" and stays there forever. No fallback, no message, no retry. The only trace is an "Uncaught (in promise)" line in the console that nobody is watching. The boundary works fine; the error simply never went through it.
The common misconception
try/catch around everything inside it. Any error thrown anywhere in its subtree, whether in rendering, a click handler or a fetch callback, ends up in the fallback.
What actually happens
A boundary catches errors from rendering
static getDerivedStateFromError, which switches it into an error state, and usually componentDidCatch, which gets the error plus a componentStack for logging. It works because React itself is calling your components. When one throws, React is right there, finds the nearest boundary above it, and renders the fallback in its place.
Handlers and async code run outside rendering
setTimeout or requestAnimationFrame callbacks, server-side rendering, and errors thrown in the boundary itself rather than its children. By the time a click handler runs, rendering has already finished and the screen is in a valid state. A .then callback or the code after an await runs even later, on its own. When that code throws, there's no render in progress for React to abandon, so the error goes to the browser instead: an uncaught exception, or for an async function, a rejected promise that nothing handles.
handleClick is async, chargeCard rejects, and the line that would reset the button never runs. The boundary sits right above the button and never hears about it.
React 19 can route an action's error to the boundary
startTransition from useTransition throws or returns a rejected promise, the fallback of the nearest boundary is displayed. A makes React the caller of that work, so it can route the failure to the boundary like a render error:
import { useState } from "react";// Rendered inside <ErrorBoundary fallback={<CheckoutError />}>export function PayButton({ orderId }: { orderId: string }) { const [paying, setPaying] = useState(false); async function handleClick() { setPaying(true); await chargeCard(orderId); // if this rejects, no boundary sees it setPaying(false); // never runs: stuck on "Paying…" } return ( <button onClick={handleClick} disabled={paying}> {paying ? "Paying…" : "Pay"} </button> );}import { useTransition } from "react";// Rendered inside <ErrorBoundary fallback={<CheckoutError />}>export function PayButton({ orderId }: { orderId: string }) { const [isPending, startTransition] = useTransition(); function handleClick() { startTransition(async () => { await chargeCard(orderId); // a rejection shows the fallback }); } return ( <button onClick={handleClick} disabled={isPending}> {isPending ? "Paying…" : "Pay"} </button> );}<form action>, which also runs in a transition, and to a rejected promise read with use, which goes to the nearest boundary. The docs add that use can't be wrapped in try/catch; a boundary is how you handle that error.
try/catch and show an inline message next to the button. Keep the boundary for what you didn't expect.
componentDidCatch | ||
try/catch with an inline message, or use a transition | ||
setTimeout, requestAnimationFrame, a .then callback | ||
startTransition from useTransition | useTransition | |
use | use | |
How to apply it in your project
Find async handlers without a catch Search for asynchandlers andonClick={async. Everyawaitin them is a place where a rejection skips your boundary. Give each one atry/catchor move the work into a transition.Decide per failure: inline or fallback Validation errors, declined payments and missing records belong in an inline message with a retry. Unexpected bugs can go to a boundary through a transition, a form action or use.Place boundaries where a fallback makes sense The docs' example is a messaging app: one boundary around the conversation list and one around each message, but not one around every avatar. Add a top-level boundary for everything else. Wire up reporting for both paths Use onCaughtErrorandonUncaughtErroron the root for errors React sees, pluswindowlisteners for handler and async errors. Throw a test error from a click handler and from a render, and confirm both reach your error reporting.
Key takeaways
An Error Boundary catches errors thrown while React renders the components below it, not everything inside it. Event handlers, timers and promise callbacks run outside rendering, so their errors skip the boundary. In React 19, errors from useTransition'sstartTransition, form actions and promises read withusedo reach it.Handle expected failures inline, keep the boundary for the unexpected, and report the errors it never sees.
التعليقات (0)