الانتقال إلى المحتوى
FrontendReact· 7 دقائق للقراءة

What Error Boundaries Don't Catch: Event Handlers, Async Code and More

A boundary catches what breaks while React renders. Clicks, timers and promises need their own plan.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • React 19
محتويات المقال

The checkout page is wrapped in an 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

The belief: an is like a 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.

It's easy to believe because the JSX looks that way: the boundary visibly wraps the button, so surely it wraps what the button does. Testing usually confirms it, since the quickest way to try a boundary is a component that throws during render, and that case works perfectly. And the word "boundary" suggests a fence around a region of the app, not around one particular phase of React's work.

What actually happens

A boundary catches errors from rendering

React's docs describe the job precisely: if a component throws during rendering, React removes the UI by default, and a boundary lets you show fallback UI for the part that crashed instead. A boundary is a class component with 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

The docs list what boundaries don't catch: event handlers, asynchronous code such as 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.

That's the checkout bug. 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

There's a documented exception. If a function passed to 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:

TSX PayButton.tsx
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>  );}

React now tracks the pending state, so there's no flag to forget to reset, and a rejected payment replaces the checkout with the boundary's fallback. The same routing applies to a function passed to <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.

Not every failure deserves a fallback, though. A declined card is something the user can fix, and replacing the whole checkout is a harsh way to say so. For expected failures, catch the error in the handler with try/catch and show an inline message next to the button. Keep the boundary for what you didn't expect.

Where the error is thrownReaches the nearest boundary?What to do
Rendering a component below the boundaryYesShow a fallback and log it in componentDidCatch
An event handler, sync or asyncNotry/catch with an inline message, or use a transition
setTimeout, requestAnimationFrame, a .then callbackNoCatch it where it runs
A function passed to startTransition from useTransitionYesWrap the component that calls useTransition
A rejected promise read with useYesWrap the component that calls use
The boundary's own renderingNo, it goes to the next one upKeep fallbacks simple, and add a top-level boundary

How to apply it in your project

  1. Find async handlers without a catchSearch for async handlers and onClick={async. Every await in them is a place where a rejection skips your boundary. Give each one a try/catch or move the work into a transition.
  2. Decide per failure: inline or fallbackValidation 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.
  3. Place boundaries where a fallback makes senseThe 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.
  4. Wire up reporting for both pathsUse onCaughtError and onUncaughtError on the root for errors React sees, plus window listeners 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's startTransition, form actions and promises read with use do reach it.
  • Handle expected failures inline, keep the boundary for the unexpected, and report the errors it never sees.

📚 المصادر

  1. Catching rendering errors with an Error Boundary — React
  2. useTransition — React API Reference
  3. use — React API Reference
  4. createRoot — React DOM API Reference
  5. Window: unhandledrejection event — MDN Web Docs

التعليقات (0)