import * as React from "react"; import { Empty} from "antd"; import CustomIcon from "../config/iconConfig"; interface State { error?: Error errorInfo?: React.ErrorInfo crashed?: boolean timer?: any } export type ErrorChild = React.ReactNode | ((error: Error, errorInfo: React.ErrorInfo) => React.ReactNode) export default function withCatchable(WrappedComponent: React.ComponentType, errorChild?: ErrorChild): React.ComponentType { return class Catchable extends React.Component { constructor(props: T) { super(props); this.state = { error: null, errorInfo: null, crashed: false, timer: 1, }; } componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { this.setState({ error, errorInfo, crashed: true }); if (console && console.group) { console.group(`%c组件${WrappedComponent.name || ""}崩溃了|Compnent Crashed`, "font-size:20px;color:#ff0047"); console.error(error); console.error(errorInfo.componentStack); console.groupEnd() } } componentWillUnmount(){ clearTimeout(this.state.timer); } private renderErrorChild = () => { try { const { error, errorInfo } = this.state; const timer = setTimeout(()=>{ this.setState({crashed: false}); },20000); this.setState({timer}) return typeof errorChild === "function" ? errorChild(error, errorInfo) : (errorChild ||
} imageStyle={{ height: 60, }} description={ 嵌入网页包含不合法的请求,。
请修改网页地址并刷新页面
} >
); } catch (e) { return Error Child Component Crashed; } } render() { return ( {this.state.crashed ? this.renderErrorChild() : } ) } } }