import { Component, ReactElement } from 'react'; import { Button } from 'antd-mobile'; interface State { hasError: boolean; } interface Props { children: ReactElement; } export default class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError() { return { hasError: true }; } componentDidCatch(error: Error, info: React.ErrorInfo) { console.log(error); console.log(info); } handleRetry = () => { this.setState({ hasError: false }); location.reload(); }; render() { if (this.state.hasError) { return (

不好意思,页面发生错误!

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