1 |
|
2 | import React from "react";
|
3 | import { AlertOutlined } from "@ant-design/icons";
|
4 |
|
5 | export 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 |
|
28 | const 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 | };
|