diff --git a/.changeset/smooth-sloths-exist.md b/.changeset/smooth-sloths-exist.md new file mode 100644 index 0000000000..6c6a91dcbc --- /dev/null +++ b/.changeset/smooth-sloths-exist.md @@ -0,0 +1,6 @@ +--- +"react-router-dom": patch +"react-router-dom-v5-compat": patch +--- + +Allow falsy `location.state` values passed to `` diff --git a/contributors.yml b/contributors.yml index b8254acaa9..61d4e5de64 100644 --- a/contributors.yml +++ b/contributors.yml @@ -254,3 +254,4 @@ - yracnet - yuleicul - zheng-chuang +- jungwoo3490 \ No newline at end of file diff --git a/packages/react-router-dom-v5-compat/lib/components.tsx b/packages/react-router-dom-v5-compat/lib/components.tsx index cdf5e7eb1b..d9e232967e 100644 --- a/packages/react-router-dom-v5-compat/lib/components.tsx +++ b/packages/react-router-dom-v5-compat/lib/components.tsx @@ -86,7 +86,7 @@ export function StaticRouter({ pathname: locationProp.pathname || "/", search: locationProp.search || "", hash: locationProp.hash || "", - state: locationProp.state || null, + state: locationProp.state != null ? locationProp.state : null, key: locationProp.key || "default", }; diff --git a/packages/react-router-dom/__tests__/static-location-test.tsx b/packages/react-router-dom/__tests__/static-location-test.tsx index 102e35da6e..016e80e6e1 100644 --- a/packages/react-router-dom/__tests__/static-location-test.tsx +++ b/packages/react-router-dom/__tests__/static-location-test.tsx @@ -56,5 +56,31 @@ describe("A ", () => { key: expect.any(String), }); }); + + it("retains a non-null state when passed explicitly", () => { + let location!: ReturnType; + function LocationChecker() { + location = useLocation(); + return null; + } + + ReactDOMServer.renderToStaticMarkup( + + + } /> + + + ); + + expect(location).toEqual({ + pathname: "/the/path", + search: "?the=query", + hash: "", + state: 0, + key: expect.any(String), + }); + }); }); }); diff --git a/packages/react-router-dom/server.tsx b/packages/react-router-dom/server.tsx index 9fcb91b173..3d12d02c89 100644 --- a/packages/react-router-dom/server.tsx +++ b/packages/react-router-dom/server.tsx @@ -66,7 +66,7 @@ export function StaticRouter({ pathname: locationProp.pathname || "/", search: locationProp.search || "", hash: locationProp.hash || "", - state: locationProp.state || null, + state: locationProp.state != null ? locationProp.state : null, key: locationProp.key || "default", };