Performance: Error boundaries concept
MEDIUM IMPACT
Error boundaries affect the user experience by preventing the entire React app from crashing when a component error occurs, improving interaction responsiveness.
class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error) { return { hasError: true }; } render() { if (this.state.hasError) { return <div>Something went wrong.</div>; } return this.props.children; } } function BuggyComponent() { throw new Error('Crash!'); } function App() { return ( <ErrorBoundary> <BuggyComponent /> </ErrorBoundary> ); }
function BuggyComponent() { throw new Error('Crash!'); return <div>Won't render</div>; } function App() { return <BuggyComponent />; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No error boundary | Full tree unmount and remount on error | Multiple reflows due to full app reload | High paint cost due to blank screen and reload | [X] Bad |
| With error boundary | Only error boundary subtree replaced | Single reflow limited to fallback UI | Low paint cost with stable UI fallback | [OK] Good |