Performance: Using error boundaries
MEDIUM IMPACT
Error boundaries affect the user experience during runtime errors by preventing full app crashes and controlling UI fallback rendering.
class ErrorBoundary extends React.Component { state = { hasError: false }; static getDerivedStateFromError() { return { hasError: true }; } render() { if (this.state.hasError) return <FallbackUI />; return this.props.children; } } function App() { return <ErrorBoundary><ComponentThatMayThrow /></ErrorBoundary>; }
function App() { return <ComponentThatMayThrow />; } // No error boundary used
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No error boundary | Full React tree unmount and remount | Multiple reflows for entire app | High paint cost due to full UI redraw | [X] Bad |
| Error boundary used | Only error subtree re-renders | Single reflow limited to subtree | Lower paint cost with partial UI update | [OK] Good |