From eb3067734b66f14dabccd3839b9412ccf7f68f8f Mon Sep 17 00:00:00 2001 From: David Williams Date: Thu, 7 Apr 2022 11:44:06 -0400 Subject: [PATCH 1/2] Fix: respect the replace prop if it is defined --- .../__tests__/link-push-test.tsx | 61 +++++++++++++++++++ packages/react-router-dom/index.tsx | 16 ++++- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/packages/react-router-dom/__tests__/link-push-test.tsx b/packages/react-router-dom/__tests__/link-push-test.tsx index 564ee9e8b..305f1f100 100644 --- a/packages/react-router-dom/__tests__/link-push-test.tsx +++ b/packages/react-router-dom/__tests__/link-push-test.tsx @@ -222,4 +222,65 @@ describe("Link push and replace", () => { `); }); }); + + describe("to the same page with replace={false}, when it is clicked", () => { + it("performs a push", () => { + function Home() { + return ( +
+

Home

+ + Home + + +
+ ); + } + + function About() { + return

About

; + } + + let renderer: TestRenderer.ReactTestRenderer; + TestRenderer.act(() => { + renderer = TestRenderer.create( + + + } /> + } /> + + + ); + }); + + let anchor = renderer.root.findByType("a"); + + TestRenderer.act(() => { + anchor.props.onClick( + new MouseEvent("click", { + view: window, + bubbles: true, + cancelable: true, + }) + ); + }); + + expect(renderer.toJSON()).toMatchInlineSnapshot(` +
+

+ Home +

+ + Home + +

+ PUSH +

+
+ `); + }); + }); }); diff --git a/packages/react-router-dom/index.tsx b/packages/react-router-dom/index.tsx index 83ecd0e83..5c7d85a41 100644 --- a/packages/react-router-dom/index.tsx +++ b/packages/react-router-dom/index.tsx @@ -261,7 +261,15 @@ export interface LinkProps */ export const Link = React.forwardRef( function LinkWithRef( - { onClick, reloadDocument, replace = false, state, target, to, ...rest }, + { + onClick, + reloadDocument, + replace = undefined, + state, + target, + to, + ...rest + }, ref ) { let href = useHref(to); @@ -411,9 +419,11 @@ export function useLinkClickHandler( event.preventDefault(); // If the URL hasn't changed, a regular will do a replace instead of - // a push, so do the same here. + // a push, so do the same here unless the replace prop is explcitly set let replace = - !!replaceProp || createPath(location) === createPath(path); + replaceProp !== undefined + ? replaceProp + : createPath(location) === createPath(path); navigate(to, { replace, state }); } From ed6deb84c74ba922980602cbfc33c4d773a6623f Mon Sep 17 00:00:00 2001 From: David Williams <35264112+williamsdyyz@users.noreply.github.com> Date: Thu, 7 Apr 2022 11:48:35 -0400 Subject: [PATCH 2/2] Update contributors.yml --- contributors.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/contributors.yml b/contributors.yml index 5a2b49d68..32fd1ef18 100644 --- a/contributors.yml +++ b/contributors.yml @@ -50,3 +50,4 @@ - underager - vijaypushkin - rtmann +- williamsdyyz