-
Notifications
You must be signed in to change notification settings - Fork 64
/
ErrorBoundary.jsx
47 lines (38 loc) · 1018 Bytes
/
ErrorBoundary.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { logError } from '../logging';
import ErrorPage from './ErrorPage';
/**
* Error boundary component used to log caught errors and display the error page.
*
* @memberof module:React
* @extends {Component}
*/
class ErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError() {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
componentDidCatch(error, info) {
logError(error, { stack: info.componentStack });
}
render() {
if (this.state.hasError) {
return this.props.fallbackComponent || <ErrorPage />;
}
return this.props.children;
}
}
ErrorBoundary.propTypes = {
children: PropTypes.node,
fallbackComponent: PropTypes.node,
};
ErrorBoundary.defaultProps = {
children: null,
fallbackComponent: undefined,
};
export default ErrorBoundary;