import { Component, type ErrorInfo, type ReactNode } from 'react' import { AlertTriangle, RefreshCw } from 'lucide-react' import Button from './Button' interface Props { children: ReactNode fallback?: ReactNode } interface State { hasError: boolean error: Error | null } export class ErrorBoundary extends Component { constructor(props: Props) { super(props) this.state = { hasError: false, error: null } } static getDerivedStateFromError(error: Error): State { return { hasError: true, error } } componentDidCatch(error: Error, errorInfo: ErrorInfo) { console.error('ErrorBoundary caught an error:', error, errorInfo) } handleReset = () => { this.setState({ hasError: false, error: null }) } render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback } return (

出错了

{this.state.error?.message || '发生了一个意外错误'}

) } return this.props.children } }