Skip to content

Commit 79babe9

Browse files
authored
fix(react): Make fallback render types more accurate (#7198)
1 parent 5359ba9 commit 79babe9

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

packages/react/src/errorboundary.tsx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export const UNKNOWN_COMPONENT = 'unknown';
1313

1414
export type FallbackRender = (errorData: {
1515
error: Error;
16-
componentStack: string | null;
17-
eventId: string | null;
16+
componentStack: string;
17+
eventId: string;
1818
resetError(): void;
1919
}) => React.ReactElement;
2020

@@ -48,11 +48,17 @@ export type ErrorBoundaryProps = {
4848
beforeCapture?(scope: Scope, error: Error | null, componentStack: string | null): void;
4949
};
5050

51-
type ErrorBoundaryState = {
52-
componentStack: React.ErrorInfo['componentStack'] | null;
53-
error: Error | null;
54-
eventId: string | null;
55-
};
51+
type ErrorBoundaryState =
52+
| {
53+
componentStack: null;
54+
error: null;
55+
eventId: null;
56+
}
57+
| {
58+
componentStack: React.ErrorInfo['componentStack'];
59+
error: Error;
60+
eventId: string;
61+
};
5662

5763
const INITIAL_STATE = {
5864
componentStack: null,
@@ -133,12 +139,17 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
133139

134140
public render(): React.ReactNode {
135141
const { fallback, children } = this.props;
136-
const { error, componentStack, eventId } = this.state;
142+
const state = this.state;
137143

138-
if (error) {
144+
if (state.error) {
139145
let element: React.ReactElement | undefined = undefined;
140146
if (typeof fallback === 'function') {
141-
element = fallback({ error, componentStack, resetError: this.resetErrorBoundary, eventId });
147+
element = fallback({
148+
error: state.error,
149+
componentStack: state.componentStack,
150+
resetError: this.resetErrorBoundary,
151+
eventId: state.eventId,
152+
});
142153
} else {
143154
element = fallback;
144155
}

0 commit comments

Comments
 (0)