Skip to content

Commit

Permalink
Properly handle falsy error values in ErrorBoundary's
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Nov 29, 2023
1 parent 30917ae commit f84eca5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
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

0 comments on commit f84eca5

Please sign in to comment.