Skip to the page

Component patterns · mid

Error boundaries

An error boundary is a class component (getDerivedStateFromError / componentDidCatch) that catches errors thrown while rendering its children and shows a fallback; it does not catch errors in event handlers, async code, or in itself.

The notes

What you must know

  • Catches: errors during render, in lifecycle methods and constructors of components below it; also rejected promises read with use() and errors thrown in transitions/Actions.
  • Does not catch: event handlers, setTimeout/promises outside React, errors in the boundary itself, server-side errors handled by the framework.
  • For event and async errors: try/catch and set error state, or rethrow during render (setState(() => { throw err })) so a boundary catches it.
  • Still class-only in React itself; react-error-boundary is the common library. Frameworks add their own (Next.js error.tsx, catchError).
  • Place boundaries per widget or route section, not only one at the root, so one broken widget does not blank the page.
  • Reset: change its key, or a resetKeys-style API, or a "Try again" button that clears the error state (Next.js retry() re-fetches server content).
  • React 19 root options onCaughtError, onUncaughtError, onRecoverableError on createRoot/hydrateRoot are the place to report to Sentry; errors are logged once, not twice.

The red pen

Common traps: mistake → fix

  • Mistake: "Error boundaries catch all errors"

    Fix: not event handlers or async code outside React.

  • Mistake: "One boundary at the root is enough"

    Fix: the whole app shows the fallback for one widget error.

  • Mistake: "You can write an error boundary with hooks"

    Fix: not in React itself; use a class or a library.

Easy to hard

Questions interviewers ask, easy to hard

  1. What does an error boundary catch and what does it not catch?
  2. How do you handle an error thrown in onClick or after an await?
  3. Why is it still class-only?
  4. How do you reset a boundary?
  5. Where do you report errors to Sentry in React 19?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview