import { Component, ErrorInfo, ReactNode } from 'react'; import { Button } from '@/components/ui/button'; interface Props { children: ReactNode } interface State { error: Error | null } /** 捕获渲染异常,避免整页白屏 */ export default class ErrorBoundary extends Component { state: State = { error: null }; static getDerivedStateFromError(error: Error) { return { error }; } componentDidCatch(error: Error, info: ErrorInfo) { console.error('[Jiang13Forum]', error, info.componentStack); } render() { if (this.state.error) { return (

页面渲染出错

{this.state.error.message}

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