typescriptintermediate
React Error Boundary Component
Build a reusable error boundary with retry, fallback UI, and error reporting integration.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { Component, type ReactNode, type ErrorInfo } from 'react';
interface Props {
children: ReactNode;
fallback?: ReactNode | ((error: Error, reset: () => void) => ReactNode);
onError?: (error: Error, info: ErrorInfo) => void;
}
interface State {
error: Error | null;
}
class ErrorBoundary extends Component<Props, State> {
state: State = { error: null };
static getDerivedStateFromError(error: Error): State {
return { error };
}
componentDidCatch(error: Error, info: ErrorInfo) {
this.props.onError?.(error, info);
}
reset = () => this.setState({ error: null });
render() {
const { error } = this.state;
if (!error) return this.props.children;
if (typeof this.props.fallback === 'function') {
return this.props.fallback(error, this.reset);
}
return (
this.props.fallback ?? (
<div role="alert" style={{ padding: '2rem', textAlign: 'center' }}>
<h2>Something went wrong</h2>
<pre style={{ color: 'red' }}>{error.message}</pre>
<button onClick={this.reset}>Try again</button>
</div>
)
);
}
}
// Usage
// <ErrorBoundary
// onError={(err) => reportToSentry(err)}
// fallback={(error, reset) => (
// <div>
// <p>Error: {error.message}</p>
// <button onClick={reset}>Retry</button>
// </div>
// )}
// >
// <MyComponent />
// </ErrorBoundary>
export default ErrorBoundary;Use Cases
- Preventing full-page crashes from component errors
- Providing retry functionality for failed renders
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
Error Boundary with Fallback UI
Class-based error boundary component that catches render errors and displays a customizable fallback UI.
Best for: Graceful error recovery
#error-boundary#error-handling
typescriptintermediate
Error Boundary with Fallback UI
Error boundary class component with customizable fallback UI, retry button, and error reporting.
Best for: Graceful error recovery
#error-boundary#error-handling
typescriptintermediate
Retry with Exponential Backoff
Retry failed async operations with exponential backoff, jitter, and configurable retry conditions.
Best for: API call resilience
#nodejs#retry
typescriptintermediate
Next.js Error Handling Patterns
Implement error.tsx, not-found.tsx, and global-error.tsx for comprehensive error handling.
Best for: Graceful error recovery in nested layouts
#nextjs#error-handling