Skip to content

Commit 3095f4b

Browse files
authored
feat(react): Support render props in ErrorBoundary (#3793)
Extend the ErrorBoundary component to allow it to support render props children. This means that patterns like the following are viable: ```jsx <Sentry.ErrorBoundary> {() => ( <Suspense fallback={<div>Loading...</div>}> <Other /> </Suspense> )} </Sentry.ErrorBoundary> ``` Fixes #3052
1 parent 8063fbb commit 3095f4b

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

packages/react/src/errorboundary.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
156156
};
157157

158158
public render(): React.ReactNode {
159-
const { fallback } = this.props;
159+
const { fallback, children } = this.props;
160160
const { error, componentStack, eventId } = this.state;
161161

162162
if (error) {
@@ -171,7 +171,10 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
171171
return null;
172172
}
173173

174-
return this.props.children;
174+
if (typeof children === 'function') {
175+
return children();
176+
}
177+
return children;
175178
}
176179
}
177180

packages/react/test/errorboundary.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ describe('ErrorBoundary', () => {
132132
expect(container.innerHTML).toBe('<h1>children</h1>');
133133
});
134134

135+
it('supports rendering children as a function', () => {
136+
const { container } = render(
137+
<ErrorBoundary fallback={<h1>Error Component</h1>}>{() => <h1>children</h1>}</ErrorBoundary>,
138+
);
139+
140+
expect(container.innerHTML).toBe('<h1>children</h1>');
141+
});
142+
135143
describe('fallback', () => {
136144
it('renders a fallback component', async () => {
137145
const { container } = render(

0 commit comments

Comments
 (0)