React · Frontend system thinking
What is an error boundary and what does it catch?
Short answer
An error boundary is a component that catches errors thrown while rendering its child tree and shows a fallback. It does not catch event handler, async, server-rendering or its own errors.
React needs a way to prevent one broken component from taking down the whole page. An error boundary wraps a subtree, records that an error occurred, and renders a fallback instead. The boundary must be a class component because it relies on getDerivedStateFromError and componentDidCatch.
class ErrorBoundary extends React.Component {
state = { hasError: false };
static getDerivedStateFromError() {
return { hasError: true };
}
componentDidCatch(error, info) {
reportError(error, info.componentStack);
}
render() {
if (this.state.hasError) {
return <p>Something went wrong.</p>;
}
return this.props.children;
}
}What is caught
- Errors thrown during rendering a descendant
- Errors in lifecycle methods of a descendant
- Errors in a descendant's constructor
- Errors in a client component during hydration, if a boundary is above it
What is not caught
- Event handlers, because they run outside React's render phase
- Asynchronous code such as a rejected promise in an effect
- Errors on the server during server rendering
- Errors thrown in the boundary itself
Place boundaries around meaningful regions, such as a route, a widget or a sidebar, rather than around the entire app. A boundary that never resets will keep showing the fallback after the user navigates or retries, so key it by route or by a retry counter.
react-error-boundary package provides a hook-friendly wrapper, but the underlying mechanism is still a class error boundary. There is no built-in hook that replaces it.Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why can't a hook be an error boundary?
- 2How do you reset an error boundary after a retry?
- 3How do you handle errors in async effects?
- 4Where would you place error boundaries in a large app?
Say it in the interview
An error boundary catches render-time errors in its child tree and shows a fallback instead of crashing the page. I use class components or the react-error-boundary wrapper, place them around routes or independent widgets, and reset them when the user retries or navigates. They do not catch event handler errors, rejected promises or server-render failures, so those need their own handling.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live React mock interview and get scored feedback on depth, clarity and confidence.