import { Component, ErrorInfo, ReactNode } from 'react' interface Props { children: ReactNode fallback?: ReactNode } interface State { hasError: boolean error: Error | null } export class ErrorBoundary extends Component { 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) } public render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback } return (

EXCEPTION

页面出现错误

{this.state.error?.message || '未知错误'}

) } return this.props.children } }