Skip to content

Commit 0a0df12

Browse files
committed
tweak: ErrorBoundary
1 parent e601a5f commit 0a0df12

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from 'react';
2+
3+
// 在 dev 下似乎没有用,会被 HMR 拦截
4+
export default class ErrorBoundary extends React.Component {
5+
static defaultProps = {
6+
onError: null,
7+
enable: true,
8+
};
9+
static getDerivedStateFromError(error) {
10+
return { hasError: true };
11+
}
12+
constructor(props) {
13+
super(props);
14+
this.state = {
15+
hasError: false,
16+
};
17+
}
18+
componentDidCatch(error, info) {
19+
const { onError } = this.props;
20+
console.error('Umi UI mini error', error);
21+
if (onError && typeof onError === 'function') {
22+
onError(error, info);
23+
}
24+
}
25+
render() {
26+
const { children, enable } = this.props;
27+
const { hasError } = this.state;
28+
if (hasError && enable) {
29+
return null;
30+
}
31+
return children;
32+
}
33+
}

packages/umi-plugin-ui/src/bubble/index.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Bubble from './Bubble';
99
import Modal from './Modal';
1010
import Loading from './Loading';
1111
import Error from './Error';
12+
import ErrorBoundary from './ErrorBoundary';
1213

1314
const winPath = input => {
1415
if (!input) {
@@ -250,6 +251,11 @@ doc.body.appendChild(node);
250251

251252
export default props => {
252253
if (!isMobile(navigator.userAgent)) {
253-
ReactDOM.render(<App {...props} />, node);
254+
ReactDOM.render(
255+
<ErrorBoundary>
256+
<App {...props} />
257+
</ErrorBoundary>,
258+
node,
259+
);
254260
}
255261
};

0 commit comments

Comments
 (0)