import { Component, type ReactNode } from 'react'; interface Props { children: ReactNode; } interface State { hasError: boolean; error: string; } export class ErrorBoundary extends Component { state: State = { hasError: false, error: '' }; static getDerivedStateFromError(e: Error): State { console.error('[ErrorBoundary]', e); return { hasError: true, error: e.message }; } componentDidCatch(_e: Error, info: React.ErrorInfo) { console.error('[ErrorBoundary] componentStack:', info.componentStack); } render() { if (this.state.hasError) { return (

应用出错

{this.state.error}

); } return this.props.children; } }