Sub topic 4
Navigating with Link and NavLink
This chapter assumes you can write React function components and JSX, and that you have a router set up with createBrowserRouter and RouterProvider. You should also know what a route object is. Here you will replace plain anchor tags with Link and NavLink so navigation stays inside the app.
Why not a plain anchor tag
A regular <a href="/about"> asks the browser to request a new page from the server. The browser discards your React tree, reloads the JavaScript, and loses all component state. A router app should handle the URL change itself.
Link renders an <a> element, but it intercepts the click. It calls event.preventDefault(), updates the URL through the History API, and tells the router to render the matching route. The page does not reload.
Using Link
Link takes a to prop. A path that starts with / is absolute. A path without a leading slash is relative to the route that renders the link.
import { Link } from "react-router-dom";
function Nav() {
return (
<nav>
<Link to="/">Home</Link> <Link to="/about">About</Link>
</nav>
);
}Use Link for any link to a route in your own app. Use a normal <a> for external sites, because the router cannot handle those URLs.
The replace prop swaps the current history entry instead of adding a new one. The Back button then skips the page the user left. This is useful after actions such as a login.
Highlighting the current page with NavLink
NavLink is a Link that knows whether its to matches the current URL. It adds the class active automatically. It also sets aria-current="page" on the active link, which helps screen readers.
For custom styling, pass a function to className or style. The function receives an object with isActive and isPending flags.
import { NavLink } from "react-router-dom";
function UserLink({ id }) {
return (
<NavLink
to={`/users/${id}`}
className={({ isActive }) => (isActive ? "active" : undefined)}
>
User {id}
</NavLink>
);
}isPending is true while the route's loader is still running. You can use it to show that a click was registered.
The "Home link is always active" problem
NavLink matches by prefix. A link to / matches every URL, so it looks active on all pages. Add the end prop so the link is active only on an exact match.
<NavLink to="/" end>Home</NavLink>The same applies to a parent link such as /users when you want it inactive on /users/42.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Using `<a href>` for internal links | Full page reload, state lost | Use `Link` |
| Forgetting `end` on a link to `/` | Home link always looks active | Add `end` |
| Using `Link` for an external URL | The router treats it as an app path | Use `<a href>` |
| Rendering a `Link` outside the router | Error about missing router context | Render it inside `RouterProvider` |
Exercise
Your navigation bar has <NavLink to="/">Home</NavLink> and <NavLink to="/about">About</NavLink>. On the About page, both links are highlighted. Why does this happen, and how do you fix it?
Show answer
NavLink matches by prefix, and every URL begins with /, so the Home link counts as active on every page. Add the end prop to the Home link: <NavLink to="/" end>Home</NavLink>. It then matches only the exact path /.