Error Boundary
In one line
An error boundary is a class component that catches JavaScript errors anywhere in its child component tree and displays a fallback UI.
In simple words
Error boundaries are React components that catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them. When an error is caught, the boundary shows a fallback UI instead of the crashed component, and the rest of the app keeps working.
Error boundaries are currently class components only — they implement static getDerivedStateFromError(error) (to update state) and/or componentDidCatch(error, info) (to log the error). You wrap any subtree with an error boundary to isolate failures.
Note: error boundaries do not catch errors in event handlers, async code, or setTimeout callbacks — those need try/catch. They catch only rendering errors.
Example
class ErrorBoundary extends React.Component {
state = { hasError: false };
static getDerivedStateFromError(error) {
return { hasError: true };
}
render() {
if (this.state.hasError) return <h1>Something went wrong.</h1>;
return this.props.children;
}
}If any child throws during render, getDerivedStateFromError sets hasError to true, and the boundary shows a fallback message.
Related terms
Related Resources
Related Lessons
- Error Boundaries
Learn error boundaries for graceful error handling.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.