Error Boundaries catch JavaScript errors in component trees displaying fallback UI instead of crashing.
Class-Based Error Boundary
typescript
class ErrorBoundary extends React.Component {
static getDerivedStateFromError(error) {
return { hasError: true };
}
render() {
if (this.state.hasError) {
return <div>Something went wrong</div>;
}
return this.props.children;
}
}Error Boundaries make apps more resilient.