Full answer
By default, an error thrown while React renders a component unmounts the entire tree and the user sees a blank screen. An error boundary limits the damage. It wraps a part of the UI, such as a widget or a route, and replaces only that part with a fallback.
An error boundary is a class component with at least one of two methods. static getDerivedStateFromError(error) runs during render and returns the new state, for example { error }, so the next render shows the fallback. componentDidCatch(error, info) runs after the fact and is the place to send the error and the component stack to a logging service.
A boundary catches errors thrown in rendering, in lifecycle methods and in constructors of everything below it. It does not catch errors in event handlers (use try and catch there), in asynchronous code such as setTimeout or promises, in server-side rendering, or errors thrown by the boundary itself. A failed boundary bubbles to the next one above.
There is no built-in hook version. Function-component projects normally install react-error-boundary, which offers an ErrorBoundary component, a fallback render prop and useErrorBoundary for sending an async error into the nearest boundary. That hook comes from the library, not from React.
React 19 adds root options for reporting. createRoot(container, { onCaughtError, onUncaughtError, onRecoverableError }) lets an app log errors that boundaries caught, errors that nothing caught, and recoverable errors such as hydration mismatches, in one place. React 19 also no longer reports an error twice.
Place boundaries at meaningful seams: one around the whole app as a last resort, and smaller ones around features that can fail independently. Offer a retry that resets the boundary and fixes the cause, otherwise "try again" just fails again.
import { Component, useState } from 'react';
class ErrorBoundary extends Component {
state = { error: null };
static getDerivedStateFromError(error) {
return { error };
}
componentDidCatch(error) {
console.log('logged: ' + error.message);
}
reset = () => this.setState({ error: null });
render() {
if (this.state.error) {
return this.props.fallback(this.state.error, this.reset);
}
return this.props.children;
}
}
function Profile({ user }) {
return <p>Hello, {user.name.toUpperCase()}</p>;
}
export default function App() {
const [user, setUser] = useState({ name: 'Asha' });
return (
<>
<button onClick={() => setUser(null)}>Load missing user</button>
<ErrorBoundary
fallback={(error, reset) => (
<div role="alert">
<p>Something went wrong: {error.message}</p>
<button
onClick={() => {
setUser({ name: 'Asha' });
reset();
}}
>
Try again
</button>
</div>
)}
>
<Profile user={user} />
</ErrorBoundary>
</>
);
}Pressing the first button sets user to null, so Profile throws while rendering. The boundary catches it, logs it and shows the fallback. Try again restores the user and resets the boundary. Run output: the logged error, then the markup showing the fallback: logged: Cannot read properties of null (reading 'name') after error: <button>Load missing user</button><div role="alert"><p>Something went wrong: Cannot read properties of null (reading 'name')</p><button>Try again</button></div>
Key points
- A boundary is a class with getDerivedStateFromError and, usually, componentDidCatch.
- It catches render, lifecycle and constructor errors in its subtree, not event handlers, async code or SSR.
- Use several boundaries so one failing widget does not hide the whole page.
- Function-component code usually uses the react-error-boundary library. useErrorBoundary is from that library.
- React 19 root options onCaughtError, onUncaughtError and onRecoverableError give central reporting.
Common mistakes
- Expecting a boundary to catch errors thrown inside onClick handlers or promises.
- Wrapping the entire app in a single boundary and losing the whole page for one small failure.
- Offering a reset button that does not change the cause, so the same error returns at once.
- Importing useErrorBoundary and calling it a React hook. It comes from react-error-boundary.
Follow-ups
- How would you send an error from a fetch call into the nearest error boundary?
- Where would you log errors in React 19, and what is the difference between onCaughtError and onUncaughtError?
- How do error boundaries interact with Suspense boundaries?
Tips
- List what boundaries do not catch. Interviewers use that as a test.
- Say clearly that boundaries are still classes, and give react-error-boundary as the practical route.
- Mention placement strategy, not only the code.