Performance: Why error boundaries matter
MEDIUM IMPACT
Error boundaries affect user experience by preventing full app crashes and reducing layout shifts caused by unhandled errors.
import { ErrorBoundary } from 'react-error-boundary'; function ErrorFallback() { return <div role="alert">Something went wrong.</div>; } function MyComponent() { const data = JSON.parse('invalid json'); return <div>{data.name}</div>; } export default function App() { return ( <ErrorBoundary FallbackComponent={ErrorFallback}> <MyComponent /> </ErrorBoundary> ); }
function MyComponent() { // No error boundary const data = JSON.parse('invalid json'); // throws error return <div>{data.name}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No error boundary, error crashes app | Full tree unmount and remount | Multiple reflows due to layout shifts | High paint cost from blank screen and re-render | [X] Bad |
| Error boundary with fallback UI | Only fallback UI rendered | Single reflow, minimal layout shift | Low paint cost, stable UI | [OK] Good |