You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
951 B
35 lines
951 B
import React, { Component } from 'react';
|
|
import { Result, Button } from 'antd';
|
|
|
|
class ErrorBoundary extends Component {
|
|
constructor(props) {
|
|
super(props);
|
|
this.state = { hasError: false };
|
|
}
|
|
|
|
static getDerivedStateFromError (error) {
|
|
// 更新 state 使下一次渲染能够显示降级后的 UI
|
|
return { hasError: true };
|
|
}
|
|
|
|
componentDidCatch (error, errorInfo) {
|
|
// 可以将错误日志上报给服务器
|
|
}
|
|
|
|
render () {
|
|
const { errorRender } = this.props;
|
|
if (this.state.hasError) {
|
|
// 可以自定义降级后的 UI 并渲染
|
|
return errorRender || <Result
|
|
status="error"
|
|
title="页面出错了哦!"
|
|
subTitle="请联系管理员进行修复"
|
|
// extra={<Button onClick={() => this.state.navigate(-1)}>返回上一页</Button>}
|
|
/>;
|
|
}
|
|
|
|
return this.props.children;
|
|
}
|
|
}
|
|
|
|
export default ErrorBoundary;
|
|
|