Skip to content

Lesson 10 of 14 · 16m

Error Boundaries and Failure Containment

Since React 16 a render crash unmounts the whole tree — class boundaries, per-panel granularity, and React 19's root error callbacks.

Since React 16, an uncaught error during render doesn't corrupt a subtree — it unmounts the entire application. That was the deliberate change: a partially-rendered UI in an unknown state was judged worse than no UI at all. The consequence for anyone shipping React is that a single broken JSON.parse in a leaf component takes the whole page down. Error boundaries are the containment system — and most codebases have exactly one, at the root, which is barely better than nothing.

What a boundary actually is

A boundary is a class component — still, in React 19; there is no hook equivalent — implementing static getDerivedStateFromError (render the fallback) and componentDidCatch (log it). react-error-boundary wraps that boilerplate into <ErrorBoundary fallback={...}> plus a resetKeys/onReset for retry, and is what production codebases use.

<ErrorBoundary
  fallback={<PanelError onRetry={() => refetch()} />}
  onError={(e, info) => reportToSentry(e, info.componentStack)}
>
  <InvoiceChart />
</ErrorBoundary>

Placement is the engineering decision. One root boundary means one widget's crash blanks the whole dashboard; per-route or per-panel boundaries mean the chart dies and the rest of the page survives — the same isolation argument as Suspense boundaries. The right granularity is "the largest unit whose loss doesn't take the page with it."

react-error-boundary also handles the reset lifecycle: resetKeys={[itemId]} remounts the boundary's children when a dependency changes (navigating to a new item clears the error state automatically), and onReset runs cleanup when the user retries — the two cases that otherwise leave a boundary stuck on its fallback after the condition that caused it is gone.

What boundaries do not catch

Event handlers, setTimeout callbacks, and async code throw outside React's render/commit cycle — boundaries never see them. onClick that parses a malformed response needs its own try/catch; this is the most common misunderstanding about what a boundary protects. Server-side errors and errors in the boundary itself also escape.

React 19 added root-level options that make the reporting story explicit: createRoot(el, { onCaughtError, onUncaughtError, onRecoverableError }) — the first fires for errors a boundary caught, the second for errors nothing caught (the ones that unmounted your app), the third for recoverable internal errors. Wire Sentry there once and every boundary reports for free, without per-boundary instrumentation.

The fallback is a feature, not a spinner

"Something went wrong" with no recovery is a dead end the user can only fix by reloading. A real fallback: what failed, a retry that clears the boundary's error state (and resetKeys for the prop-driven equivalent — changing the retrying list id resets the boundary automatically), and a way to keep working when the widget is optional. The fallback is also where Sentry breadcrumbs earn their budget — componentDidCatch's info.componentStack is the stack trace with real component names, which is what turns "white screen" reports into a traceable bug instead of a shrug.

Where the limit actually is

Boundaries don't make errors recoverable — they make them contained. If the failing component holds shared state the rest of the page depends on, its failure is still a page-level problem regardless of where the boundary sits. The design follows the failure domain: wrap the parts that can fail independently, and accept that some trees fail as a unit by nature.

Field notes

  • Boundary ≠ data-fetching error state. A 404 from the API is a value, rendered deliberately; a boundary is for thrown errors in render.
  • In development StrictMode remounts and can rethrow into the boundary; test boundaries in a production build before declaring them broken.
  • Don't boundary every component — the per-boundary fallback is a layout decision, not a coverage metric.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.