diff --git a/.changeset/fluffy-buttons-push.md b/.changeset/fluffy-buttons-push.md new file mode 100644 index 0000000000..38efa8a730 --- /dev/null +++ b/.changeset/fluffy-buttons-push.md @@ -0,0 +1,5 @@ +--- +"react-router-dom": patch +--- + +Respect relative=path prop on NavLink diff --git a/contributors.yml b/contributors.yml index 82ff65bb5b..b23ea4c66c 100644 --- a/contributors.yml +++ b/contributors.yml @@ -72,6 +72,7 @@ - loun4 - lqze - lukerSpringTree +- manzano78 - marc2332 - markivancho - marvinruder diff --git a/packages/react-router-dom/__tests__/nav-link-active-test.tsx b/packages/react-router-dom/__tests__/nav-link-active-test.tsx index 34851b7d9d..8b9da3c449 100644 --- a/packages/react-router-dom/__tests__/nav-link-active-test.tsx +++ b/packages/react-router-dom/__tests__/nav-link-active-test.tsx @@ -394,6 +394,33 @@ describe("NavLink", () => { }); }); }); + + describe("when it matches with relative=path links", () => { + it("applies the default 'active' className to the underlying ", () => { + let renderer: TestRenderer.ReactTestRenderer; + TestRenderer.act(() => { + renderer = TestRenderer.create( + + + + Link + + } + /> + + + ); + }); + + let anchor = renderer.root.findByType("a"); + + expect(anchor.props.href).toEqual("/contacts/1"); + expect(anchor.props.className).toEqual("active"); + }); + }); }); describe("NavLink using a data router", () => { diff --git a/packages/react-router-dom/index.tsx b/packages/react-router-dom/index.tsx index c782becb5a..9c1e46fda3 100644 --- a/packages/react-router-dom/index.tsx +++ b/packages/react-router-dom/index.tsx @@ -441,7 +441,7 @@ export const NavLink = React.forwardRef( }, ref ) { - let path = useResolvedPath(to); + let path = useResolvedPath(to, { relative: rest.relative }); let match = useMatch({ path: path.pathname, end, caseSensitive }); let routerState = React.useContext(DataRouterStateContext);