Files
jiang13-forum/frontend/src/components/ErrorBoundary.tsx
freefire e1c1708715 初始提交:姜十三论坛 Jiang13 Forum
轻量自用论坛,Go 单二进制 + React SPA 内嵌 + SQLite。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-15 21:08:52 +08:00

34 lines
1011 B
TypeScript

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<Props, State> {
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 (
<div style={{ padding: 24, textAlign: 'center' }}>
<h3></h3>
<p style={{ color: 'var(--color-text-3)', fontSize: 13 }}>{this.state.error.message}</p>
<Button size="sm" onClick={() => { this.setState({ error: null }); window.location.reload(); }}>
</Button>
</div>
);
}
return this.props.children;
}
}