Containing React Crashes with Strategic Error Boundaries
Error Boundaries isolate component-tree crashes so the rest of your React app stays interactive. This post shows where to place them, a reusable widget boundary pattern, and the critical gaps they don't cover — event handlers, async code, and SSR.
09 May 2026, 01:01 UTC

The Problem: One Broken Component Takes Down Everything
A single TypeError in a dashboard widget shouldn't unmount the navigation, the user menu, and the rest of the page. Yet without boundaries, React's default behavior is to unwind the entire component tree when an error bubbles up during render. Error Boundaries, introduced in React 16 and stable through React 19, let you draw a line: catch the error, log it, and show a fallback only for the subtree that failed.
How Error Boundaries Actually Work
An Error Boundary is a class component that implements one or both of two lifecycle methods:
static getDerivedStateFromError(error)— returns state to trigger a fallback rendercomponentDidCatch(error, info)— side-effect hook for logging
Function components cannot be Error Boundaries themselves. They can only be wrapped by one. This is a deliberate React design choice: the boundary must exist above the faulting subtree in the tree hierarchy.
Where to Place Them: Route-Level vs Widget-Level
A single global boundary at the root is the blunt instrument — it saves the app from a white screen but leaves the user with nothing interactive. A more surgical approach:
- Route-level boundaries around each top-level page (e.g.,
<Dashboard />,<Settings />) keep navigation alive when a page crashes. - Widget-level boundaries around independent panels (charts, feeds, third-party embeds) let the rest of the page stay functional.
This mirrors how microservices isolate failure domains. The trade-off: more boundaries mean more fallback UI states to design and more places to instrument logging.
Worked Example: A Reusable Widget Boundary
// WidgetBoundary.jsx
import React from 'react';
class WidgetBoundary extends React.Component {
state = { hasError: false, error: null };
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
componentDidCatch(error, info) {
// Send to your error tracking service
console.error('WidgetBoundary caught:', error, info.componentStack);
}
render() {
if (this.state.hasError) {
return (
This widget failed to load.
this.setState({ hasError: false, error: null })}>
Retry
);
}
return this.props.children;
}
}
export default WidgetBoundary;
Usage in a dashboard layout:
import WidgetBoundary from './WidgetBoundary';
function Dashboard() {
return (
);
}
If ThirdPartyCalendar throws during render, only its grid cell shows the fallback. The chart and feed remain interactive.
What Error Boundaries Don't Catch
This is the most common misconception. Boundaries only catch errors during:
- Render phase (including constructors)
- Lifecycle methods (
componentDidMount,componentDidUpdate, etc.) setStateupdater functions
They do not catch:
- Event handlers (onClick, onChange)
- Asynchronous callbacks (
setTimeout, Promise.then()) - Server-side rendering (Next.js
getServerSideProps, etc.) - Errors thrown inside the boundary's own
renderor fallback
For event handlers and async code, wrap the risky call in a try/catch and call this.setState({ hasError: true }) manually, or use a hook-based error reporter that posts to your logging endpoint.
Verification Checklist
- Confirm
reactandreact-domare ≥ 16.0.0 inpackage.json. - Throw
new Error('test')in a child's render; the boundary's fallback should appear while siblings stay mounted. - Open React DevTools → Components → select the boundary → verify
state.hasError === true. - Check your error tracking service (Sentry, Datadog, custom) receives the
componentStackfromcomponentDidCatch.
Closing: Start Small, Instrument Everything
Add one boundary at the route level first — it's the highest ROI. Then wrap third-party or frequently changing widgets. Pair every boundary with centralized logging so you're not debugging from user screenshots. And remember: a boundary that throws while rendering its own fallback becomes the new crash site. Keep fallbacks simple, stateless, and tested.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.