Files
tashow-h5/packages/shared/src/error-page/errorBoundary/index.tsx
2025-09-05 15:18:10 +08:00

50 lines
1.1 KiB
TypeScript

// components/ErrorBoundary/index.tsx
import React, { Component, ReactNode } from "react";
import { Result, Button } from "antd-mobile";
interface Props {
children: ReactNode;
fallback?: ReactNode;
}
interface State {
hasError: boolean;
error?: Error;
}
class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: any) {
console.error("ErrorBoundary caught an error:", error, errorInfo);
}
render() {
if (this.state.hasError) {
if (this.props.fallback) {
return this.props.fallback;
}
return (
<div style={{ padding: "20px", textAlign: "center" }}>
<Result status="error" title="页面出错了" description="抱歉,页面出现了错误" />
<Button color="primary" onClick={() => window.location.reload()}>
</Button>
</div>
);
}
return this.props.children;
}
}
export default ErrorBoundary;