Add a React Error Boundary to Isolate Component Crashes
Catch JavaScript errors in a React component tree, render fallback UI, and keep the rest of the page mounted.
17 Oct 2026, 01:53 UTC

The problem: one crashing component can unmount the whole page
When a React component throws during render, the error bubbles up through the component tree. If no component catches it, React unmounts the entire application in production. A single bad line in a user profile card can blank out the dashboard, navigation, and footer.
An error boundary is a React component that catches JavaScript errors in its child tree, logs them, and renders fallback UI instead of the crashed subtree. The rest of the page stays mounted and interactive.
Prerequisites
- React 16 or later installed in the project.
- A class component; function components cannot be error boundaries by themselves.
- Access to edit the component tree where you want isolation.
Create the error boundary
Create a class component that implements static getDerivedStateFromError to enter the error state, and optionally componentDidCatch to log the error and stack trace.
import { Component } from 'react';
export class ErrorBoundary extends Component {
state = { hasError: false, error: null, info: null };
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
componentDidCatch(error, errorInfo) {
this.setState({ info: errorInfo.componentStack });
// Send error and stack to your error reporting service here.
}
render() {
if (this.state.hasError) {
return (
Something went wrong in this section.
this.setState({ hasError: false, error: null, info: null })}
>
Reload this section
);
}
return this.props.children;
}
}
getDerivedStateFromError runs during rendering and updates state so React can show the fallback. componentDidCatch runs after the error is caught and receives the error object plus componentStack, a string representation of the component stack trace.
Wrap the subtree you want to protect
Place the boundary around the component or feature you want to isolate, not necessarily around the whole application.
import { ErrorBoundary } from './ErrorBoundary';
function Page() {
return (
);
}
If UserProfile throws during render, React unmounts only that subtree and renders the boundary's fallback. Navigation and Footer remain mounted.
Verify the boundary works
- Temporarily add throw new Error('Simulated render failure'); at the top of the child component's render method or function body.
- Reload the page in development mode.
- Check that the fallback UI appears where the child was, while surrounding components stay visible.
- Remove the simulated throw.
To confirm what the boundary does not catch, add throw new Error('Handler error'); inside an onClick handler and click it. The error boundary will not catch it; React will surface the unhandled error. Event handlers and asynchronous callbacks such as setTimeout or promises need their own try/catch.
Check your React version
Error boundary behavior has changed across React 16, 17, 18, and 19, especially with concurrent rendering. Check the installed version before relying on concurrent-mode behavior.
# Run in the project root
# Requires read access to package.json
grep '"react"' package.json
Expected output resembles "react": "^18.2.0",. Compare that version against the React release notes for your deployment target. In React Developer Tools, open the Components inspector and look at the React version badge at the top of the tree.
Recovery options after an error
- Reset state: The boundary's Reload this section button calls setState to clear hasError, which causes React to re-render the children from a clean state.
- Change the child key: If the child holds stale or corrupted state that reappears on re-render, pass a new key prop to force React to remount a fresh subtree:
<UserProfile key={resetKey} userId={userId} />.
Limitations
| Situation | Caught by error boundary? |
|---|---|
| Error thrown during render of a child component | Yes |
| Error thrown in a lifecycle method of a child | Yes |
| Error thrown inside an event handler | No; use try/catch |
| Error in setTimeout, promise, or other async callback | No; use try/catch or .catch |
| Error thrown in the boundary's own render or fallback | No; propagates to the next parent boundary |
| Error during server-side rendering | No |
Avoid wrapping the entire application in one boundary. A single boundary around everything hides bugs and makes failures harder to diagnose. Place boundaries around independent features, routes, or widgets instead.
Remove the boundary (rollback)
Because adding this component changes files in the repository, roll back by deleting the boundary wrapper and importing the child directly:
<UserProfile userId={userId} />
Then delete ErrorBoundary.jsx if no other component uses it.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.