11import * as Sentry from '@sentry/browser' ;
22import * as React from 'react' ;
33
4+ export const FALLBACK_ERR_MESSAGE = 'No fallback component has been set' ;
5+
46export type ErrorBoundaryProps = {
57 fallback ?: React . ReactNode ;
68 fallbackRender ?( error : Error | null , componentStack : string | null , resetErrorBoundary : ( ) => void ) : React . ReactNode ;
79 onError ?( error : Error , componentStack : string ) : void ;
10+ onMount ?( error : Error | null , componentStack : string | null ) : void ;
811 onReset ?( error : Error | null , componentStack : string | null ) : void ;
12+ onUnmount ?( error : Error | null , componentStack : string | null ) : void ;
913} ;
1014
1115type ErrorBoundaryState = {
@@ -33,6 +37,22 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
3337 this . setState ( { error, componentStack } ) ;
3438 }
3539
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+
3656 public resetErrorBoundary = ( ) => {
3757 const { onReset } = this . props ;
3858 if ( onReset ) {
@@ -53,7 +73,7 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
5373 return fallback ;
5474 }
5575
56- throw new Error ( 'No fallback component has been set' ) ;
76+ throw new Error ( FALLBACK_ERR_MESSAGE ) ;
5777 }
5878
5979 return this . props . children ;
0 commit comments