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

Fix: respect the replace prop if it is defined #8779

Merged
merged 4 commits into from Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions contributors.yml
Expand Up @@ -53,3 +53,5 @@
- turansky
- underager
- vijaypushkin
- rtmann
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^^ I don't think this is intentional - potentially leftover from the merge conflict resolution?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. It got sorted not added.

- williamsdyyz
61 changes: 61 additions & 0 deletions packages/react-router-dom/__tests__/link-push-test.tsx
Expand Up @@ -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 (
<div>
<h1>Home</h1>
<Link to="." replace={false}>
Home
</Link>
<ShowNavigationType />
</div>
);
}

function About() {
return <h1>About</h1>;
}

let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/home"]}>
<Routes>
<Route path="home" element={<Home />} />
<Route path="about" element={<About />} />
</Routes>
</MemoryRouter>
);
});

let anchor = renderer.root.findByType("a");

TestRenderer.act(() => {
anchor.props.onClick(
new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: true,
})
);
});

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<div>
<h1>
Home
</h1>
<a
href="/home"
onClick={[Function]}
>
Home
</a>
<p>
PUSH
</p>
</div>
`);
});
});
});
16 changes: 13 additions & 3 deletions packages/react-router-dom/index.tsx
Expand Up @@ -261,7 +261,15 @@ export interface LinkProps
*/
export const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(
function LinkWithRef(
{ onClick, reloadDocument, replace = false, state, target, to, ...rest },
{
onClick,
reloadDocument,
replace = undefined,
williamsdyyz marked this conversation as resolved.
Show resolved Hide resolved
state,
target,
to,
...rest
},
ref
) {
let href = useHref(to);
Expand Down Expand Up @@ -411,9 +419,11 @@ export function useLinkClickHandler<E extends Element = HTMLAnchorElement>(
event.preventDefault();

// If the URL hasn't changed, a regular <a> 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 });
}
Expand Down