Skip to content

Commit 987109d

Browse files
committed
Allow returning undefined from actions and loaders
1 parent 2d34a7e commit 987109d

File tree

4 files changed

+19
-43
lines changed

4 files changed

+19
-43
lines changed

.changeset/fluffy-ducks-try.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router-dom": major
3+
---
4+
5+
Allow returning `undefined` from actions and loaders

packages/react-router/__tests__/router/router-test.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1987,7 +1987,7 @@ describe("a router", () => {
19871987
router.dispose();
19881988
});
19891989

1990-
it("throws an error if actions/loaders return undefined", async () => {
1990+
it("allows returning undefined from actions/loaders", async () => {
19911991
let t = setup({
19921992
routes: [
19931993
{
@@ -2008,12 +2008,10 @@ describe("a router", () => {
20082008
location: {
20092009
pathname: "/path",
20102010
},
2011-
errors: {
2012-
path: new Error(
2013-
'You defined a loader for route "path" but didn\'t return anything ' +
2014-
"from your `loader` function. Please return a value or `null`."
2015-
),
2011+
loaderData: {
2012+
path: undefined,
20162013
},
2014+
errors: null,
20172015
});
20182016

20192017
await t.navigate("/");
@@ -2029,16 +2027,18 @@ describe("a router", () => {
20292027
formData: createFormData({}),
20302028
});
20312029
await nav3.actions.path.resolve(undefined);
2030+
await nav3.loaders.path.resolve("PATH");
20322031
expect(t.router.state).toMatchObject({
20332032
location: {
20342033
pathname: "/path",
20352034
},
2036-
errors: {
2037-
path: new Error(
2038-
'You defined an action for route "path" but didn\'t return anything ' +
2039-
"from your `action` function. Please return a value or `null`."
2040-
),
2035+
actionData: {
2036+
path: undefined,
2037+
},
2038+
loaderData: {
2039+
path: "PATH",
20412040
},
2041+
errors: null,
20422042
});
20432043
});
20442044
});

packages/react-router/__tests__/router/ssr-test.ts

+3-25
Original file line numberDiff line numberDiff line change
@@ -2065,33 +2065,11 @@ describe("ssr", () => {
20652065
expect(data).toBe("");
20662066
});
20672067

2068-
it("should error if an action/loader returns undefined", async () => {
2068+
it("should allow returning undefined from an action/loader", async () => {
20692069
let T = setupFlexRouteTest();
2070-
let data;
2071-
2072-
try {
2073-
data = await T.resolveLoader(undefined);
2074-
} catch (e) {
2075-
data = e;
2076-
}
2077-
expect(data).toEqual(
2078-
new Error(
2079-
'You defined a loader for route "flex" but didn\'t return anything ' +
2080-
"from your `loader` function. Please return a value or `null`."
2081-
)
2082-
);
20832070

2084-
try {
2085-
data = await T.resolveAction(undefined);
2086-
} catch (e) {
2087-
data = e;
2088-
}
2089-
expect(data).toEqual(
2090-
new Error(
2091-
'You defined an action for route "flex" but didn\'t return anything ' +
2092-
"from your `action` function. Please return a value or `null`."
2093-
)
2094-
);
2071+
expect(await T.resolveLoader(undefined)).toBeUndefined();
2072+
expect(await T.resolveAction(undefined)).toBeUndefined();
20952073
});
20962074

20972075
it("should handle relative redirect responses (loader)", async () => {

packages/react-router/lib/router/router.ts

-7
Original file line numberDiff line numberDiff line change
@@ -4398,13 +4398,6 @@ async function callLoaderOrAction(
43984398
} else {
43994399
result = await runHandler(handler);
44004400
}
4401-
4402-
invariant(
4403-
result.result !== undefined,
4404-
`You defined ${type === "action" ? "an action" : "a loader"} for route ` +
4405-
`"${match.route.id}" but didn't return anything from your \`${type}\` ` +
4406-
`function. Please return a value or \`null\`.`
4407-
);
44084401
} catch (e) {
44094402
// We should already be catching and converting normal handler executions to
44104403
// HandlerResults and returning them, so anything that throws here is an

0 commit comments

Comments
 (0)