"use client" import React from "react" interface Props { children: React.ReactNode fallback?: React.ReactNode } interface State { hasError: boolean error?: Error } export class ErrorBoundary extends React.Component { constructor(props: Props) { super(props) this.state = { hasError: false } } static getDerivedStateFromError(error: Error): State { return { hasError: true, error } } componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { console.error("ErrorBoundary 捕获错误:", error, errorInfo) } render() { if (this.state.hasError) { return ( this.props.fallback || (

组件加载失败

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

) ) } return this.props.children } }