Error Boundaries and Recovery
Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.
What you will be able to do
#- Explain exactly what an error boundary catches.
- Implement a boundary that shows a fallback and reports the error.
- Reset a boundary so a recovered region can retry.
Overview
#What an error boundary catches
An error boundary catches errors thrown during rendering, in lifecycle methods and in the constructors of its descendants. It does not catch errors in event handlers, asynchronous callbacks, timers or the boundary itself. Those are normal JavaScript exceptions.
The class requirement
Error boundaries must be class components because they use two lifecycle methods. getDerivedStateFromError updates state so the next render shows a fallback. componentDidCatch reports the error.
class ErrorBoundary extends React.Component<Props, { hasError: boolean }> {
state = { hasError: false };
static getDerivedStateFromError() { return { hasError: true }; }
componentDidCatch(error: Error, info: React.ErrorInfo) {
reportError(error, info.componentStack);
}
render() {
if (this.state.hasError) return this.props.fallback;
return this.props.children;
}
}
Granular boundaries
A single boundary at the root means one failure blanks the whole app. Place boundaries around independent regions so a failing widget shows a fallback while the rest of the page keeps working.
Resetting a boundary
Once a boundary has caught an error it stays in the error state. To let the user retry, reset it. A common approach is to give the boundary a key that changes on retry, which remounts it and clears the error.
<ErrorBoundary key={attempt} fallback={<Retry onRetry={() => setAttempt((a) => a + 1)} />}>
<Widget />
</ErrorBoundary>
Root options in React 19
createRoot accepts onCaughtError, onUncaughtError and onRecoverableError so you can route caught, uncaught and recoverable errors to a logger in one place.
Pitfall: expecting boundaries to catch everything
Async errors, event handler errors and errors in the boundary itself are not caught. Handle those with try/catch, a global listener, or a promise rejection handler.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
An error is thrown inside onClick. Does the boundary catch it?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.tsx<ErrorBoundary fallback={<Oops />}> <button onClick={() => { throw new Error('boom'); }}>Go</button> </ErrorBoundary>Exercise 2Implement
Implement an error boundary that shows a fallback and reports the error.
starter.tsxexport class Boundary extends React.Component<{ children: React.ReactNode; fallback: React.ReactNode }, { hasError: boolean }> { render() { return this.props.children; } }Reveal the reference answer
reference.tsxexport class Boundary extends React.Component<{ children: React.ReactNode; fallback: React.ReactNode }, { hasError: boolean }> { state = { hasError: false }; static getDerivedStateFromError() { return { hasError: true }; } componentDidCatch(error: Error, info: React.ErrorInfo) { reportError(error, info.componentStack); } render() { if (this.state.hasError) return this.props.fallback; return this.props.children; } }Authored expected output: A thrown child shows the fallback and the error is reported with its component stack.
getDerivedStateFromError flips the state so the next render shows the fallback, and componentDidCatch receives the error and component stack for logging.
Exercise 3Debug
After one failure this widget stays broken even when the data recovers. Explain and fix.
starter.tsx<Boundary fallback={<Oops />}><Widget data={data} /></Boundary>Reveal the reference answer
reference.tsx<Boundary key={data.id} fallback={<Oops />}><Widget data={data} /></Boundary>Authored expected output: When data.id changes, the boundary remounts and retries the widget.
A boundary stays in its error state until reset. Changing the key remounts the boundary, clearing hasError and letting the widget render again for the new data.
Exercise 4Explain
Explain what error boundaries do not catch and how you handle those cases.
starter.tsx// render errors vs async and event errorsReveal the reference answer
reference.tsxError boundaries catch errors during rendering, in lifecycle methods and in constructors of descendants. They do not catch errors in event handlers, asynchronous callbacks such as promises and timeouts, server-side rendering, or the boundary itself. Handle event handler errors with try/catch, promise rejections with a rejection handler, and unexpected global errors with a window error listener. React 19 root options onCaughtError and onUncaughtError give a single place to log both caught and uncaught errors.Authored expected output: A written list of what is not caught plus the handling mechanism for each.
The boundary is scoped to the render lifecycle. Anything outside it needs ordinary JavaScript error handling.
Common misconceptions
#- Error boundaries catch all errors. They catch render, lifecycle and constructor errors in descendants only.
- A boundary can be a function component. It must be a class with getDerivedStateFromError or componentDidCatch.
- A caught boundary resets itself. It stays in the error state until you reset it, for example with a key change.
- One root boundary is enough. Granular boundaries keep the rest of the page usable when one region fails.
Follow-up practice
#- react-hydration
- react-component-testing
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.