Commit 8a0b62be by 姜雷

添加错误捕获页面

parent c0ec03dd
import React from 'react';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { error: null, errorInfo: null };
}
componentDidCatch(error, errorInfo) {
// Catch errors in any components below and re-render with error message
this.setState({
error: error,
errorInfo: errorInfo,
});
// You can also log error messages to an error reporting service here
}
render() {
if (this.state.errorInfo) {
// Error path
return (
<div>
<h2>Something went wrong.</h2>
<details style={{ whiteSpace: 'pre-wrap' }}>
{this.state.error && this.state.error.toString()}
<br />
{this.state.errorInfo.componentStack}
</details>
</div>
);
}
// Normally, just render children
return this.props.children;
}
}
export default ErrorBoundary;
...@@ -3,6 +3,8 @@ import { Switch, Route, Redirect, NavLink } from 'react-router-dom'; ...@@ -3,6 +3,8 @@ import { Switch, Route, Redirect, NavLink } from 'react-router-dom';
import styles from './style.css'; import styles from './style.css';
import ReprtRepair from './ReprtRepair'; import ReprtRepair from './ReprtRepair';
import RepairList from './RepairList'; import RepairList from './RepairList';
import ErrorBoundary from '../ErrorCatch/ErrorBoundary';
class index extends Component { class index extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
...@@ -30,6 +32,7 @@ class index extends Component { ...@@ -30,6 +32,7 @@ class index extends Component {
render() { render() {
const { showFooter } = this.state; const { showFooter } = this.state;
return ( return (
<ErrorBoundary>
<div className="Content-Wrap"> <div className="Content-Wrap">
<div className="RepairContent"> <div className="RepairContent">
<Switch> <Switch>
...@@ -58,6 +61,7 @@ class index extends Component { ...@@ -58,6 +61,7 @@ class index extends Component {
</div> </div>
)} )}
</div> </div>
</ErrorBoundary>
); );
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment