Skip to content

Commit 64a3af4

Browse files
committed
feat(react): Add lifecycle hooks to errorboundary
1 parent a0dff70 commit 64a3af4

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

packages/react/src/errorboundary.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import * as Sentry from '@sentry/browser';
22
import * as React from 'react';
33

4+
export const FALLBACK_ERR_MESSAGE = 'No fallback component has been set';
5+
46
export 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

1115
type 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

Comments
 (0)