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.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe 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/catchand set error state, or rethrow during render (setState(() => { throw err })) so a boundary catches it. - Still class-only in React itself;
react-error-boundaryis the common library. Frameworks add their own (Next.jserror.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 aresetKeys-style API, or a "Try again" button that clears the error state (Next.jsretry()re-fetches server content). - React 19 root options
onCaughtError,onUncaughtError,onRecoverableErroroncreateRoot/hydrateRootare 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
- What does an error boundary catch and what does it not catch?
- How do you handle an error thrown in
onClickor after anawait? - Why is it still class-only?
- How do you reset a boundary?
- 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 voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.