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

Allow returning undefined from actions and loaders #11680

Merged
merged 1 commit into from
Jun 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fluffy-ducks-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router-dom": major
---

Allow returning `undefined` from actions and loaders
22 changes: 11 additions & 11 deletions packages/react-router/__tests__/router/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,7 @@ describe("a router", () => {
router.dispose();
});

it("throws an error if actions/loaders return undefined", async () => {
it("allows returning undefined from actions/loaders", async () => {
let t = setup({
routes: [
{
Expand All @@ -2008,12 +2008,10 @@ describe("a router", () => {
location: {
pathname: "/path",
},
errors: {
path: new Error(
'You defined a loader for route "path" but didn\'t return anything ' +
"from your `loader` function. Please return a value or `null`."
),
loaderData: {
path: undefined,
},
errors: null,
});

await t.navigate("/");
Expand All @@ -2029,16 +2027,18 @@ describe("a router", () => {
formData: createFormData({}),
});
await nav3.actions.path.resolve(undefined);
await nav3.loaders.path.resolve("PATH");
expect(t.router.state).toMatchObject({
location: {
pathname: "/path",
},
errors: {
path: new Error(
'You defined an action for route "path" but didn\'t return anything ' +
"from your `action` function. Please return a value or `null`."
),
actionData: {
path: undefined,
},
loaderData: {
path: "PATH",
},
errors: null,
});
});
});
Expand Down
28 changes: 3 additions & 25 deletions packages/react-router/__tests__/router/ssr-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2065,33 +2065,11 @@ describe("ssr", () => {
expect(data).toBe("");
});

it("should error if an action/loader returns undefined", async () => {
it("should allow returning undefined from an action/loader", async () => {
let T = setupFlexRouteTest();
let data;

try {
data = await T.resolveLoader(undefined);
} catch (e) {
data = e;
}
expect(data).toEqual(
new Error(
'You defined a loader for route "flex" but didn\'t return anything ' +
"from your `loader` function. Please return a value or `null`."
)
);

try {
data = await T.resolveAction(undefined);
} catch (e) {
data = e;
}
expect(data).toEqual(
new Error(
'You defined an action for route "flex" but didn\'t return anything ' +
"from your `action` function. Please return a value or `null`."
)
);
expect(await T.resolveLoader(undefined)).toBeUndefined();
expect(await T.resolveAction(undefined)).toBeUndefined();
});

it("should handle relative redirect responses (loader)", async () => {
Expand Down
7 changes: 0 additions & 7 deletions packages/react-router/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4398,13 +4398,6 @@ async function callLoaderOrAction(
} else {
result = await runHandler(handler);
}

invariant(
result.result !== undefined,
`You defined ${type === "action" ? "an action" : "a loader"} for route ` +
`"${match.route.id}" but didn't return anything from your \`${type}\` ` +
`function. Please return a value or \`null\`.`
);
} catch (e) {
// We should already be catching and converting normal handler executions to
// HandlerResults and returning them, so anything that throws here is an
Expand Down