1
1
import * as Sentry from '@sentry/browser' ;
2
2
import * as React from 'react' ;
3
3
4
+ export const FALLBACK_ERR_MESSAGE = 'No fallback component has been set' ;
5
+
4
6
export type ErrorBoundaryProps = {
5
7
fallback ?: React . ReactNode ;
6
8
fallbackRender ?( error : Error | null , componentStack : string | null , resetErrorBoundary : ( ) => void ) : React . ReactNode ;
7
9
onError ?( error : Error , componentStack : string ) : void ;
10
+ onMount ?( error : Error | null , componentStack : string | null ) : void ;
8
11
onReset ?( error : Error | null , componentStack : string | null ) : void ;
12
+ onUnmount ?( error : Error | null , componentStack : string | null ) : void ;
9
13
} ;
10
14
11
15
type ErrorBoundaryState = {
@@ -33,6 +37,22 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
33
37
this . setState ( { error, componentStack } ) ;
34
38
}
35
39
40
+ public componentDidMount ( ) : void {
41
+ const { error, componentStack } = this . state ;
42
+ const { onMount } = this . props ;
43
+ if ( onMount ) {
44
+ onMount ( error , componentStack ) ;
45
+ }
46
+ }
47
+
48
+ public componentWillUnmount ( ) : void {
49
+ const { error, componentStack } = this . state ;
50
+ const { onUnmount } = this . props ;
51
+ if ( onUnmount ) {
52
+ onUnmount ( error , componentStack ) ;
53
+ }
54
+ }
55
+
36
56
public resetErrorBoundary = ( ) => {
37
57
const { onReset } = this . props ;
38
58
if ( onReset ) {
@@ -53,7 +73,7 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
53
73
return fallback ;
54
74
}
55
75
56
- throw new Error ( 'No fallback component has been set' ) ;
76
+ throw new Error ( FALLBACK_ERR_MESSAGE ) ;
57
77
}
58
78
59
79
return this . props . children ;
0 commit comments