UNPKG

1 kBJavaScriptView Raw
1/* eslint-disable no-unused-vars */
2import React from "react";
3import { AlertOutlined } from "@ant-design/icons";
4
5export default class ErrorBoundary extends React.Component {
6 constructor(props) {
7 super(props);
8 this.props = props;
9 this.state = { hasError: false };
10 }
11
12 static getDerivedStateFromError() {
13 return { hasError: true };
14 }
15
16 componentDidCatch(error, errorInfo) {
17 // 你同样可以将错误日志上报给服务器
18 }
19
20 render() {
21 if (this.state.hasError) {
22 return <Holder {...this.props} />;
23 }
24 return this.props.children;
25 }
26}
27
28const Holder = ({ style, title = "出错了" }) => {
29 return (
30 <div
31 style={{
32 padding: "15px",
33 display: "flex",
34 justifyContent: "center",
35 alignItems: "center",
36 background: "white",
37 ...style,
38 }}
39 >
40 <AlertOutlined style={{ fontSize: "18px", marginRight: "5px" }} />
41 <span style={{ letterSpacing: "1px" }}>{title}</span>
42 </div>
43 );
44};