Skip to content

Commit b23d7b5

Browse files
authored
Fix docs on NavLink w.r.t. links to the root route and the end prop (#10757)
* Fix docs on NavLink w.r.t. links to the root route and the end prop * Update
1 parent a0d6c4f commit b23d7b5

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

docs/components/nav-link.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,29 +84,17 @@ You can pass a render prop as children to customize the content of the `<NavLink
8484

8585
The `end` prop changes the matching logic for the `active` and `pending` states to only match to the "end" of the NavLink's `to` path. If the URL is longer than `to`, it will no longer be considered active.
8686

87-
Without the end prop, this link is always active because every URL matches `/`.
88-
89-
```tsx
90-
<NavLink to="/">Home</NavLink>
91-
```
92-
93-
To match the URL "to the end" of `to`, use `end`:
94-
95-
```tsx
96-
<NavLink to="/" end>
97-
Home
98-
</NavLink>
99-
```
100-
101-
Now this link will only be active at `"/"`. This works for paths with more segments as well:
102-
10387
| Link | URL | isActive |
10488
| ----------------------------- | ------------ | -------- |
10589
| `<NavLink to="/tasks" />` | `/tasks` | true |
10690
| `<NavLink to="/tasks" />` | `/tasks/123` | true |
10791
| `<NavLink to="/tasks" end />` | `/tasks` | true |
10892
| `<NavLink to="/tasks" end />` | `/tasks/123` | false |
10993

94+
**A note on links to the root route**
95+
96+
`<NavLink to="/">` is an exceptional case because _every_ URL matches `/`. To avoid this matching every single route by default, it effectively ignores the `end` prop and only matches when you're at the root route.
97+
11098
## `caseSensitive`
11199

112100
Adding the `caseSensitive` prop changes the matching logic to make it case sensitive.

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,42 @@ describe("NavLink", () => {
317317
expect(anchors.map((a) => a.props.className)).toEqual(["active", ""]);
318318
});
319319

320+
it("matches the root route with or without the end prop", () => {
321+
let renderer: TestRenderer.ReactTestRenderer;
322+
TestRenderer.act(() => {
323+
renderer = TestRenderer.create(
324+
<MemoryRouter>
325+
<Routes>
326+
<Route index element={<NavLink to="/">Root</NavLink>} />
327+
</Routes>
328+
</MemoryRouter>
329+
);
330+
});
331+
332+
let anchor = renderer.root.findByType("a");
333+
expect(anchor.props.className).toMatch("active");
334+
335+
TestRenderer.act(() => {
336+
renderer = TestRenderer.create(
337+
<MemoryRouter>
338+
<Routes>
339+
<Route
340+
index
341+
element={
342+
<NavLink to="/" end>
343+
Root
344+
</NavLink>
345+
}
346+
/>
347+
</Routes>
348+
</MemoryRouter>
349+
);
350+
});
351+
352+
anchor = renderer.root.findByType("a");
353+
expect(anchor.props.className).toMatch("active");
354+
});
355+
320356
it("does not automatically apply to root non-layout segments", () => {
321357
let renderer: TestRenderer.ReactTestRenderer;
322358
TestRenderer.act(() => {

0 commit comments

Comments
 (0)