File tree Expand file tree Collapse file tree 2 files changed +40
-1
lines changed
packages/umi-plugin-ui/src/bubble Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import Bubble from './Bubble';
9
9
import Modal from './Modal' ;
10
10
import Loading from './Loading' ;
11
11
import Error from './Error' ;
12
+ import ErrorBoundary from './ErrorBoundary' ;
12
13
13
14
const winPath = input => {
14
15
if ( ! input ) {
@@ -250,6 +251,11 @@ doc.body.appendChild(node);
250
251
251
252
export default props => {
252
253
if ( ! isMobile ( navigator . userAgent ) ) {
253
- ReactDOM . render ( < App { ...props } /> , node ) ;
254
+ ReactDOM . render (
255
+ < ErrorBoundary >
256
+ < App { ...props } />
257
+ </ ErrorBoundary > ,
258
+ node ,
259
+ ) ;
254
260
}
255
261
} ;
You can’t perform that action at this time.
0 commit comments