Skip to content

Commit b55b828

Browse files
committed
ref(react): Overload fallback prop
1 parent 6744ecc commit b55b828

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

packages/react/src/errorboundary.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,31 @@ import * as React from 'react';
44

55
export const UNKNOWN_COMPONENT = 'unknown';
66

7+
export type FallbackRender = (fallback: {
8+
error: Error | null;
9+
componentStack: string | null;
10+
resetError(): void;
11+
}) => React.ReactNode;
12+
713
export type ErrorBoundaryProps = {
814
showDialog?: boolean;
915
dialogOptions?: Sentry.ReportDialogOptions;
10-
fallback?: React.ReactNode;
11-
fallbackRender?(fallback: {
12-
error: Error | null;
13-
componentStack: string | null;
14-
resetError(): void;
15-
}): React.ReactNode;
16+
// tslint:disable-next-line: no-null-undefined-union
17+
fallback?: React.ReactNode | FallbackRender;
1618
onError?(error: Error, componentStack: string): void;
1719
onMount?(): void;
1820
onReset?(error: Error | null, componentStack: string | null): void;
1921
onUnmount?(error: Error | null, componentStack: string | null): void;
2022
};
2123

24+
/*
25+
fallbackRender?(fallback: {
26+
error: Error | null;
27+
componentStack: string | null;
28+
resetError(): void;
29+
}): React.ReactNode;
30+
*/
31+
2232
type ErrorBoundaryState = {
2333
componentStack: string | null;
2434
error: Error | null;
@@ -71,16 +81,16 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
7181
};
7282

7383
public render(): React.ReactNode {
74-
const { fallback, fallbackRender } = this.props;
84+
const { fallback } = this.props;
7585
const { error, componentStack } = this.state;
7686

7787
if (error) {
78-
if (typeof fallbackRender === 'function') {
79-
return fallbackRender({ error, componentStack, resetError: this.resetErrorBoundary });
80-
}
8188
if (React.isValidElement(fallback)) {
8289
return fallback;
8390
}
91+
if (typeof fallback === 'function') {
92+
return fallback({ error, componentStack, resetError: this.resetErrorBoundary }) as FallbackRender;
93+
}
8494

8595
// Fail gracefully if no fallback provided
8696
return null;

packages/react/test/errorboundary.test.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ describe('ErrorBoundary', () => {
4242
mockShowReportDialog.mockClear();
4343
});
4444

45-
it('renders null if not given a valid `fallbackRender` prop', () => {
46-
const { container } = render(
47-
// @ts-ignore
48-
<ErrorBoundary fallbackRender={'ok'}>
49-
<Bam />
50-
</ErrorBoundary>,
51-
);
52-
53-
expect(container.innerHTML).toBe('');
54-
});
55-
5645
it('renders null if not given a valid `fallback` prop', () => {
5746
const { container } = render(
5847
// @ts-ignore
@@ -126,12 +115,12 @@ describe('ErrorBoundary', () => {
126115
expect(container.innerHTML).toBe('<p>You have hit an error</p>');
127116
});
128117

129-
it('renders a fallbackRender component', async () => {
118+
it('renders a render props component', async () => {
130119
let errorString = '';
131120
let compStack = '';
132121
const { container } = render(
133122
<TestApp
134-
fallbackRender={({ error, componentStack }) => {
123+
fallback={({ error, componentStack }) => {
135124
if (error && componentStack) {
136125
errorString = error.toString();
137126
compStack = componentStack;
@@ -205,7 +194,7 @@ describe('ErrorBoundary', () => {
205194
const { container } = render(
206195
<TestApp
207196
onReset={mockOnReset}
208-
fallbackRender={({ resetError }) => <button data-testid="reset" onClick={resetError} />}
197+
fallback={({ resetError }) => <button data-testid="reset" onClick={resetError} />}
209198
>
210199
<h1>children</h1>
211200
</TestApp>,

0 commit comments

Comments
 (0)