Skip to content
·5 min read·By Mateusz

Error Boundaries Are Not Optional in Production

One thrown error in one component unmounts your entire React tree by default. Error boundaries contain the blast radius, and most codebases never ship them.

ReactError HandlingJavaScriptWeb Dev

React's default error behavior is brutal and most teams never notice until a customer shows them a screenshot. One thrown error anywhere in the tree:

function Price({ plan }: { plan: Plan }) {
  return <span>${plan.price.toFixed(2)}</span>;
  // plan.price is undefined on the third API response
  // -> TypeError -> React unmounts the ENTIRE tree
}

White page. No header, no navigation, no way for the user to recover except leaving. Error boundaries exist to contain that blast radius, and they are the rare React feature that is older than most of the codebases missing them.

Step 1: Know what they actually catch

Boundaries catch errors thrown during rendering, inside lifecycle methods, and in constructors:

<ErrorBoundary fallback={<WidgetError />}>
  <RiskyWidget />
</ErrorBoundary>

They do not catch event handlers, code in setTimeout or promises, server component errors, or errors thrown inside the boundary itself:

function onClick() {
  try {
    save();
  } catch (e) {
    showRetry();
  }
  // handlers need their own try/catch.
  // boundaries never see these errors
}

That gap list is why some developers conclude boundaries are useless. It is backwards. Render-time failures, a field that is undefined on the third API response, a third-party component that throws on a bad prop, are exactly the failures that take down whole pages in production, and they are the ones nothing else catches.

Step 2: Build one small reusable boundary

A class component, about 40 lines, written once per app:

class ErrorBoundary extends React.Component<
  { fallback: ReactNode; children: ReactNode },
  { hasError: boolean }
> {
  state = { hasError: false };
 
  static getDerivedStateFromError() {
    return { hasError: true };
  }
 
  componentDidCatch(error: Error, info: React.ErrorInfo) {
    monitor.capture(error, {
      componentStack: info.componentStack,
    });
  }
 
  render() {
    return this.state.hasError
      ? this.props.fallback
      : this.props.children;
  }
}

getDerivedStateFromError flips the state that renders the fallback. componentDidCatch is the logging side effect. Every team that ships without one is choosing, per feature, that a crash there should blank the page. Nobody actually chooses that. It just never came up.

Step 3: Scope boundaries to risk, not to the tree

The question is not "how deep should boundaries go" but "what breaks the page when this fails":

// wrong: one boundary around everything.
// any error blanks the entire page
<ErrorBoundary fallback={<SiteDown />}>
  <Header />
  <Main />
  <Footer />
</ErrorBoundary>
 
// right: scoped to the risky widget.
// header and checkout stay alive
<ErrorBoundary fallback={<ChartError />}>
  <ChartPanel />
</ErrorBoundary>

Charts and dashboards fed by API data, feeds and comments, third-party embeds like maps and chat widgets, anything that fetches during render. If the product can survive without the widget showing, the widget gets its own boundary.

Step 4: Give the fallback a way forward

A fallback that says "something went wrong" with no action is a white page with better branding:

function Fallback({ resetErrorBoundary }: { resetErrorBoundary: () => void }) {
  return (
    <div>
      <p>This section failed to load.</p>
      <button onClick={resetErrorBoundary}>Try again</button>
    </div>
  );
}
 
<ErrorBoundary
  resetKeys={[page.pathname]}
  onReset={() => setQueryKey((k) => k + 1)}
  fallback={<Fallback resetErrorBoundary={...} />}
>
  <Feed />
</ErrorBoundary>

The reset button clears the error state, and resetKeys clears it automatically when the route changes. Users click retry far more often than they click away.

Step 5: Log with enough context to fix it

componentDidCatch hands you the error and the component stack. Send both to your monitoring with the route and release version attached:

componentDidCatch(error: Error, info: React.ErrorInfo) {
  monitor.capture(error, {
    componentStack: info.componentStack,
    route: window.location.pathname,
    release: APP_VERSION,
  });
}

The boundary turns a customer screenshot into a stack trace, which is the entire operational argument for shipping them. Without the logging, it is just a prettier crash.

Add one root boundary as the safety net today, then start scoping them into the risky widgets. If you are not sure where your app would blank right now, that is the tell that the answer is "everywhere".

Shipping a React app that customers depend on? Error boundaries plus the logging hookup above is a day of work. Tell me about the app and I will tell you which widgets get boundaries first.

Frequently asked questions

Do error boundaries work in React 19?

Yes, unchanged. They are still class components using getDerivedStateFromError or componentDidCatch. No hook equivalent exists yet, which is why most teams write one reusable boundary component and wrap with it.

Do error boundaries catch errors in event handlers?

No. Errors in event handlers, setTimeout, promises, and other async code are not caught. Wrap that logic in try/catch. Boundaries are for render-time failures: a bad prop, a undefined field, a throwing third-party component.

Does Next.js error.js replace error boundaries?

No, it is one. error.tsx files in the App Router create error boundaries at the segment level automatically. You still want smaller scoped boundaries inside complex pages so one widget failing does not blank the whole route.

Should I wrap the whole app in one boundary?

One root boundary is a safety net, not a strategy. It prevents the white screen but any error blanks everything. Scope additional boundaries around independent widgets so a failing chart does not take the checkout flow down.

Have a project in mind?

Tell me what you are building and I will reply within 24 hours with an honest take on scope, timeline, and price.

Get a Quote

Related posts