Skip to content

Commit 69debd1

Browse files
fix: apply NavLink pending state when the to prop has a trailing slash (#15300)
1 parent 4f3d5ca commit 69debd1

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `NavLink` not applying its `pending` state when `to` has a trailing slash

packages/react-router/__tests__/dom/nav-link-active-test.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,45 @@ describe("NavLink using a data router", () => {
696696
expect(screen.getByText("Link to Bar").className).toBe("active");
697697
});
698698

699+
it("applies the 'pending' className to a NavLink whose 'to' has a trailing slash", async () => {
700+
let dfd = createDeferred();
701+
let router = createBrowserRouter(
702+
createRoutesFromElements(
703+
<Route path="/" element={<Layout />}>
704+
<Route path="home" element={<Outlet />}>
705+
<Route
706+
path="child"
707+
loader={() => dfd.promise}
708+
element={<p>Child page</p>}
709+
/>
710+
</Route>
711+
</Route>,
712+
),
713+
{
714+
window: getWindow("/"),
715+
},
716+
);
717+
render(<RouterProvider router={router} />);
718+
719+
function Layout() {
720+
return (
721+
<>
722+
<NavLink to="/home/">Home</NavLink>
723+
<NavLink to="/home/child">Child</NavLink>
724+
<Outlet />
725+
</>
726+
);
727+
}
728+
729+
expect(screen.getByText("Home").className).toBe("");
730+
731+
fireEvent.click(screen.getByText("Child"));
732+
expect(screen.getByText("Home").className).toBe("pending");
733+
734+
dfd.resolve(null);
735+
await waitFor(() => screen.getByText("Child page"));
736+
});
737+
699738
it("applies its className correctly when provided as a function", async () => {
700739
let dfd = createDeferred();
701740
let router = createBrowserRouter(

packages/react-router/lib/dom/lib.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,7 @@ export const NavLink = React.forwardRef<HTMLAnchorElement, NavLinkProps>(
16901690
(nextLocationPathname === toPathname ||
16911691
(!end &&
16921692
nextLocationPathname.startsWith(toPathname) &&
1693-
nextLocationPathname.charAt(toPathname.length) === "/"));
1693+
nextLocationPathname.charAt(endSlashPosition) === "/"));
16941694

16951695
let renderProps = {
16961696
isActive,

0 commit comments

Comments
 (0)