Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle falsy error values in ErrorBoundary's #11071

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/handle-falsy-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Properly handle falsy error values in ErrorBoundary's
44 changes: 44 additions & 0 deletions packages/react-router/__tests__/data-memory-router-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,50 @@ describe("createMemoryRouter", () => {
}
});

it("handles a `null` render-error", async () => {
let router = createMemoryRouter([
{
path: "/",
Component() {
throw null;
},
ErrorBoundary() {
return <pre>{useRouteError() === null ? "Yes" : "No"}</pre>;
},
},
]);
let { container } = render(<RouterProvider router={router} />);

await waitFor(() => screen.getByText("Yes"));
expect(getHtml(container)).toMatch("Yes");
});

it("handles a `null` render-error from a defer() call", async () => {
let router = createMemoryRouter([
{
path: "/",
loader() {
return defer({ lazy: Promise.reject(null) });
},
Component() {
let data = useLoaderData() as { lazy: Promise<unknown> };
return (
<React.Suspense>
<Await resolve={data.lazy}>No</Await>
</React.Suspense>
);
},
ErrorBoundary() {
return <pre>{useRouteError() === null ? "Yes" : "No"}</pre>;
},
},
]);
let { container } = render(<RouterProvider router={router} />);

await waitFor(() => screen.getByText("Yes"));
expect(getHtml(container)).toMatch("Yes");
});

it("handles back button routing away from a child error boundary", async () => {
let router = createMemoryRouter(
createRoutesFromElements(
Expand Down
6 changes: 3 additions & 3 deletions packages/react-router/lib/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ export class RenderErrorBoundary extends React.Component<
// this because the error provided from the app state may be cleared without
// the location changing.
return {
error: props.error || state.error,
error: props.error !== undefined ? props.error : state.error,
location: state.location,
revalidation: props.revalidation || state.revalidation,
};
Expand All @@ -610,7 +610,7 @@ export class RenderErrorBoundary extends React.Component<
}

render() {
return this.state.error ? (
return this.state.error !== undefined ? (
<RouteContext.Provider value={this.props.routeContext}>
<RouteErrorContext.Provider
value={this.state.error}
Expand Down Expand Up @@ -886,7 +886,7 @@ export function useRouteError(): unknown {

// If this was a render error, we put it in a RouteError context inside
// of RenderErrorBoundary
if (error) {
if (error !== undefined) {
return error;
}

Expand Down