mirror of
https://github.com/LHY0125/Gobang-Game.git
synced 2026-06-29 08:55:53 +08:00
feat: 添加 React Error Boundary 组件防止渲染异常白屏
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { Component, type ReactNode } from 'react';
|
||||
|
||||
interface Props {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
interface State {
|
||||
hasError: boolean;
|
||||
error: Error | null;
|
||||
}
|
||||
|
||||
export default class ErrorBoundary extends Component<Props, State> {
|
||||
constructor(props: Props) {
|
||||
super(props);
|
||||
this.state = { hasError: false, error: null };
|
||||
}
|
||||
|
||||
static getDerivedStateFromError(error: Error): State {
|
||||
return { hasError: true, error };
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.hasError) {
|
||||
return (
|
||||
<div style={{
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
height: '100vh',
|
||||
color: '#F5DEB3',
|
||||
background: '#3C2415',
|
||||
gap: 16,
|
||||
fontFamily: 'Microsoft YaHei, sans-serif',
|
||||
}}>
|
||||
<h2>程序出错了</h2>
|
||||
<pre style={{ fontSize: 14, opacity: 0.7 }}>
|
||||
{this.state.error?.message}
|
||||
</pre>
|
||||
<button
|
||||
onClick={() => {
|
||||
this.setState({ hasError: false, error: null });
|
||||
window.location.reload();
|
||||
}}
|
||||
style={{
|
||||
padding: '10px 24px',
|
||||
fontSize: 16,
|
||||
border: '2px solid #8B7355',
|
||||
borderRadius: 6,
|
||||
background: '#DEB887',
|
||||
color: '#3C2415',
|
||||
cursor: 'pointer',
|
||||
}}
|
||||
>
|
||||
重新加载
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user