Modern React Error Boundary with TypeScript

A type-safe error boundary component with fallback UI and error reporting


A modern implementation of React Error Boundary with TypeScript support, fallback UI, and error reporting capabilities.

ErrorBoundary.tsx
import React, { Component, ErrorInfo, ReactNode } from 'react';
 
interface Props {
  children: ReactNode;
  fallback?: ReactNode;
  onError?: (error: Error, errorInfo: ErrorInfo) => void;
}
 
interface State {
  hasError: boolean;
  error: Error | null;
}
 
export class ErrorBoundary extends Component<Props, State> {
  public state: State = {
    hasError: false,
    error: null,
  };
 
  public static getDerivedStateFromError(error: Error): State {
    return { hasError: true, error };
  }
 
  public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    console.error('Uncaught error:', error, errorInfo);
    
    if (this.props.onError) {
      this.props.onError(error, errorInfo);
    }
  }
 
  public render() {
    if (this.state.hasError) {
      if (this.props.fallback) {
        return this.props.fallback;
      }
 
      return (
        <div className="min-h-screen flex items-center justify-center bg-gray-50">
          <div className="max-w-md w-full p-6 bg-white rounded-lg shadow-lg">
            <h2 className="text-2xl font-bold text-red-600 mb-4">
              Something went wrong
            </h2>
            <p className="text-gray-600 mb-4">
              {this.state.error?.message || 'An unexpected error occurred'}
            </p>
            <button
              onClick={() => this.setState({ hasError: false, error: null })}
              className="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 transition-colors"
            >
              Try again
            </button>
          </div>
        </div>
      );
    }
 
    return this.props.children;
  }
}
 
// Usage example:
function App() {
  return (
    <ErrorBoundary
      onError={(error, errorInfo) => {
        // Report error to your error tracking service
        console.error('Error caught by boundary:', error, errorInfo);
      }}
      fallback={
        <div className="p-4 bg-red-100 text-red-800">
          <h2>Oops! Something went wrong</h2>
          <button onClick={() => window.location.reload()}>
            Refresh Page
          </button>
        </div>
      }
    >
      <YourApp />
    </ErrorBoundary>
  );
}

Features:

  • TypeScript support with proper type definitions
  • Customizable fallback UI
  • Error reporting callback
  • Clean, modern UI for error state
  • Retry mechanism
  • Proper error logging