Skip to content

Commit 23167c5

Browse files
committed
Relax fallback prop to support broader ReactNode type
1 parent 4aaf9b0 commit 23167c5

File tree

3 files changed

+5
-12
lines changed

3 files changed

+5
-12
lines changed

src/ErrorBoundary.test.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe("ErrorBoundary", () => {
5656
expect(container.textContent).toBe("Content");
5757
});
5858

59-
describe("callback props", () => {
59+
describe("fallback props", () => {
6060
let errorBoundaryRef: RefObject<ErrorBoundary>;
6161

6262
beforeEach(() => {
@@ -66,11 +66,7 @@ describe("ErrorBoundary", () => {
6666
function render(props: Omit<ErrorBoundaryPropsWithFallback, "fallback">) {
6767
act(() => {
6868
root.render(
69-
<ErrorBoundary
70-
{...props}
71-
fallback={<div>Error</div>}
72-
ref={errorBoundaryRef}
73-
>
69+
<ErrorBoundary {...props} fallback="Error" ref={errorBoundaryRef}>
7470
<MaybeThrows>Content</MaybeThrows>
7571
</ErrorBoundary>
7672
);

src/ErrorBoundary.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isDevelopment } from "#is-development";
2-
import { Component, createElement, ErrorInfo, isValidElement } from "react";
2+
import { Component, createElement, ErrorInfo } from "react";
33
import { ErrorBoundaryContext } from "./ErrorBoundaryContext";
44
import { ErrorBoundaryProps, FallbackProps } from "./types";
55

@@ -94,7 +94,7 @@ export class ErrorBoundary extends Component<
9494
childToRender = fallbackRender(props);
9595
} else if (FallbackComponent) {
9696
childToRender = createElement(FallbackComponent, props);
97-
} else if (fallback === null || isValidElement(fallback)) {
97+
} else if (fallback !== undefined) {
9898
childToRender = fallback;
9999
} else {
100100
if (isDevelopment) {

src/types.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ export type ErrorBoundaryPropsWithRender = ErrorBoundarySharedProps & {
3838
};
3939

4040
export type ErrorBoundaryPropsWithFallback = ErrorBoundarySharedProps & {
41-
fallback: ReactElement<
42-
unknown,
43-
string | FunctionComponent | typeof Component
44-
> | null;
41+
fallback: ReactNode;
4542
FallbackComponent?: never;
4643
fallbackRender?: never;
4744
};

0 commit comments

Comments
 (0)