File tree 4 files changed +19
-43
lines changed
4 files changed +19
-43
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " react-router-dom " : major
3
+ ---
4
+
5
+ Allow returning ` undefined ` from actions and loaders
Original file line number Diff line number Diff line change @@ -1987,7 +1987,7 @@ describe("a router", () => {
1987
1987
router . dispose ( ) ;
1988
1988
} ) ;
1989
1989
1990
- it ( "throws an error if actions/loaders return undefined " , async ( ) => {
1990
+ it ( "allows returning undefined from actions/loaders" , async ( ) => {
1991
1991
let t = setup ( {
1992
1992
routes : [
1993
1993
{
@@ -2008,12 +2008,10 @@ describe("a router", () => {
2008
2008
location : {
2009
2009
pathname : "/path" ,
2010
2010
} ,
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 ,
2016
2013
} ,
2014
+ errors : null ,
2017
2015
} ) ;
2018
2016
2019
2017
await t . navigate ( "/" ) ;
@@ -2029,16 +2027,18 @@ describe("a router", () => {
2029
2027
formData : createFormData ( { } ) ,
2030
2028
} ) ;
2031
2029
await nav3 . actions . path . resolve ( undefined ) ;
2030
+ await nav3 . loaders . path . resolve ( "PATH" ) ;
2032
2031
expect ( t . router . state ) . toMatchObject ( {
2033
2032
location : {
2034
2033
pathname : "/path" ,
2035
2034
} ,
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" ,
2041
2040
} ,
2041
+ errors : null ,
2042
2042
} ) ;
2043
2043
} ) ;
2044
2044
} ) ;
Original file line number Diff line number Diff line change @@ -2065,33 +2065,11 @@ describe("ssr", () => {
2065
2065
expect ( data ) . toBe ( "" ) ;
2066
2066
} ) ;
2067
2067
2068
- it ( "should error if an action/loader returns undefined " , async ( ) => {
2068
+ it ( "should allow returning undefined from an action/loader" , async ( ) => {
2069
2069
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
- ) ;
2083
2070
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 ( ) ;
2095
2073
} ) ;
2096
2074
2097
2075
it ( "should handle relative redirect responses (loader)" , async ( ) => {
Original file line number Diff line number Diff line change @@ -4398,13 +4398,6 @@ async function callLoaderOrAction(
4398
4398
} else {
4399
4399
result = await runHandler ( handler ) ;
4400
4400
}
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
- ) ;
4408
4401
} catch ( e ) {
4409
4402
// We should already be catching and converting normal handler executions to
4410
4403
// HandlerResults and returning them, so anything that throws here is an
You can’t perform that action at this time.
0 commit comments