1 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
2 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
3 | import _inherits from 'babel-runtime/helpers/inherits';
|
4 |
|
5 | var _class, _temp;
|
6 |
|
7 | import React from 'react';
|
8 | import PropTypes from 'prop-types';
|
9 |
|
10 | DefaultUI.propTypes = {
|
11 | error: PropTypes.object,
|
12 | errorInfo: PropTypes.object
|
13 | };
|
14 |
|
15 | function DefaultUI() {
|
16 | return '';
|
17 | }
|
18 |
|
19 | var ErrorBoundary = (_temp = _class = function (_React$Component) {
|
20 | _inherits(ErrorBoundary, _React$Component);
|
21 |
|
22 | function ErrorBoundary(props) {
|
23 | _classCallCheck(this, ErrorBoundary);
|
24 |
|
25 | var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
|
26 |
|
27 | _this.state = { error: null, errorInfo: null };
|
28 | return _this;
|
29 | }
|
30 |
|
31 | ErrorBoundary.prototype.componentDidCatch = function componentDidCatch(error, errorInfo) {
|
32 | this.setState({
|
33 | error: error,
|
34 | errorInfo: errorInfo
|
35 | });
|
36 |
|
37 | var afterCatch = this.props.afterCatch;
|
38 |
|
39 |
|
40 | if ('afterCatch' in this.props && typeof afterCatch === 'function') {
|
41 | this.props.afterCatch(error, errorInfo);
|
42 | }
|
43 | };
|
44 |
|
45 | ErrorBoundary.prototype.render = function render() {
|
46 | var _props$fallbackUI = this.props.fallbackUI,
|
47 | FallbackUI = _props$fallbackUI === undefined ? DefaultUI : _props$fallbackUI;
|
48 |
|
49 |
|
50 | if (this.state.errorInfo) {
|
51 | return React.createElement(FallbackUI, {
|
52 | error: this.state.error,
|
53 | errorInfo: this.state.errorInfo
|
54 | });
|
55 | }
|
56 |
|
57 | return this.props.children;
|
58 | };
|
59 |
|
60 | return ErrorBoundary;
|
61 | }(React.Component), _class.propTypes = {
|
62 | children: PropTypes.element,
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 | afterCatch: PropTypes.func,
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | fallbackUI: PropTypes.func
|
76 | }, _temp);
|
77 | ErrorBoundary.displayName = 'ErrorBoundary';
|
78 | export { ErrorBoundary as default }; |
\ | No newline at end of file |