Skip to content
Advanced PatternsChapter 28 / 37Intermediate5 min read

Error Boundaries

Learn how a React error boundary catches render errors so the whole app does not crash, using a class component, react-error-boundary and reset.

On this page

1. What is an Error Boundary?

In React, if a component throws an error during render and nothing catches it, React unmounts the whole root. The user sees an empty white screen.

An Error Boundary is like JavaScript's try/catch, but for a tree of components.

2. Why does it matter?

Without an Error BoundaryWith an Error Boundary
One widget throws, the whole app goes whiteOnly that widget shows a fallback
The user has no idea what happenedA clear message and a Try again button
The error is not logged anywhereError reporting in componentDidCatch
No way out except a reloadReset and render again

3. Where is it used?

PlaceWhy
Around the whole appLast line of defense, a generic error page
Around each route or pageAn error on one page does not break the rest of the navigation
Third-party widget, chart, mapIf outside code crashes, only that part is affected
Components that use use(promise)The error from a rejected Promise lands here

In the Next.js App Router, an error.tsx file creates an Error Boundary for you.

4. How does it work?

From error to fallback
A child component throws an error during render
React looks up the tree for the nearest Error Boundary
getDerivedStateFromError updates the state
The boundary renders the fallback instead of the children
componentDidCatch logs the error

5. Code example

A class component boundary

An Error Boundary is still built with a class component, because there is no hook for it.

tsx
import { Component, type ErrorInfo, type ReactNode } from "react";

type Props = { fallback: ReactNode; children: ReactNode };
type State = { hasError: boolean };

export class ErrorBoundary extends Component<Props, State> {
  state: State = { hasError: false };

  static getDerivedStateFromError(): State {
    return { hasError: true };
  }

  componentDidCatch(error: Error, info: ErrorInfo) {
    console.error(error, info.componentStack);
  }

  render() {
    if (this.state.hasError) {
      return this.props.fallback;
    }
    return this.props.children;
  }
}
tsx
<ErrorBoundary fallback={<p>Could not load the chart.</p>}>
  <SalesChart />
</ErrorBoundary>

Reset with the react-error-boundary library

tsx
import { ErrorBoundary, type FallbackProps } from "react-error-boundary";

function Fallback({ error, resetErrorBoundary }: FallbackProps) {
  return (
    <div role="alert">
      <p>Something went wrong: {error instanceof Error ? error.message : "Unknown error"}</p>
      <button onClick={resetErrorBoundary}>Try again</button>
    </div>
  );
}

export function Dashboard() {
  return (
    <ErrorBoundary FallbackComponent={Fallback}>
      <SalesChart />
    </ErrorBoundary>
  );
}

How does the code work?

PartMeaning
getDerivedStateFromErrorAs soon as an error arrives, state becomes hasError: true
componentDidCatchLogging, or reporting to a service like Sentry
info.componentStackWhich component tree the error came from
fallbackThe UI shown when there is an error
resetErrorBoundaryResets the state and tries to render the children again

React 19 also has the onCaughtError and onUncaughtError options on createRoot, where you can report all errors in one place.

6. Common mistakes

Mistake: Relying on an Error Boundary for an onClick error

Why? An event handler does not run during render, so the boundary never sees that error.

Better: Write a try catch inside the handler and keep the error in state to show it in the UI.

Mistake: Using only one boundary for the whole app

Why? An error in one small widget turns the entire page into the fallback.

Better: Keep one boundary at the top, and separate boundaries around important sections or widgets.

Mistake: Giving the fallback no way to reset

Why? The user is forced to reload the page and loses form data.

Better: Add a Try again button that resets the boundary, like resetErrorBoundary in react-error-boundary.

Interview tip

You may be asked, "Can a function component be an Error Boundary?" The answer: not by itself. So far only a class component can provide getDerivedStateFromError and componentDidCatch. For function components, the react-error-boundary library gives you a wrapper.

7. What to learn before and after

Final quick revision

PointDetail
What it isA component that catches render errors and shows a fallback
How it is builtA class component with getDerivedStateFromError
LoggingcomponentDidCatch(error, info)
Does not catchEvent handlers, async code, SSR, its own errors
Easy optionThe react-error-boundary library
Next.jsThe error.tsx file