Wrap UI sections in React Error Boundaries to catch rendering errors gracefully. Without error boundaries, a single component crash (a null reference, a failed API parse) takes down the entire page — showing users a white screen with no way to recover or navigate away.
Without error boundaries, a single component throwing during render crashes the entire React tree — users see a blank white screen with no error message, no navigation, and no way to recover except refreshing the page. This turns a minor component bug into a complete application failure.
BeforeMerge scans your pull requests against this rule and 4+ others. Get actionable feedback before code ships.
In React, an unhandled error during rendering unmounts the entire component tree. Without error boundaries, a single TypeError: Cannot read property of undefined in one small component crashes the entire page — the user sees a white screen with no indication of what happened and no way to navigate away.
This is catastrophic for user experience. The user cannot:
They must manually refresh the browser, losing any unsaved state. If the error is deterministic (e.g., bad data from the API), refreshing triggers the same crash in an infinite loop.
Error boundaries catch rendering errors in their child component tree, display a fallback UI, and keep the rest of the application functional.
Wrap distinct UI sections (pages, sidebars, widgets, data-dependent components) in error boundaries. Each boundary should display a contextual fallback UI with a retry action. Never let a single component crash take down the entire application.
// No error boundaries — one crash kills everything
export default function DashboardPage() {
return (
<div>
<Header />
<Sidebar />
<main>
<AnalyticsChart /> {/* If this crashes... */}
<RecentActivity /> {/* ...everything dies */}
<TeamMembers />
</main>
</div>
);
}import { ErrorBoundary } from "react-error-boundary";
function ErrorFallback({ error, resetErrorBoundary }: FallbackProps) {
return (
<div role="alert" className="p-4 border rounded bg-red-50">
<h3>Something went wrong</h3>
<p className="text-sm text-gray-600">{error.message}</p>
<button onClick={resetErrorBoundary}>Try again</button>
</div>
);
}
export default function DashboardPage() {
return (
<div>
<Header />
<Sidebar />
<main>
<ErrorBoundary FallbackComponent={ErrorFallback}>
<AnalyticsChart /> {/* Crash here only affects this section */}
</ErrorBoundary>
<ErrorBoundary FallbackComponent={ErrorFallback}>
<RecentActivity />
</ErrorBoundary>
<ErrorBoundary FallbackComponent={ErrorFallback}>
<TeamMembers />
</ErrorBoundary>
</main>
</div>
);
}Search for pages that lack error boundaries:
grep -rL 'ErrorBoundary\|error-boundary' app/ --include="*.tsx"Files in the app/ directory that do not import or use an error boundary are unprotected.
react-error-boundary: npm install react-error-boundaryErrorFallback component with retry functionalityErrorBoundaryerror.tsx files for route-level error handling