Sub topic 6
Navigating with Link and NavLink
This chapter assumes you know React components and props, and that you have a route config like the one from the earlier chapters. You should also know how nested routes render through Outlet. Here you will move between those routes without a full page reload.
Why not a plain anchor tag
A plain <a href="/contacts"> makes the browser request a whole new document. The page reloads and all client state is lost. Link renders the same <a> element, but React Router intercepts the click. It updates the URL, calls the loaders for the routes that changed, and swaps the matched route components.
import { Link } from "react-router";
export default function Home() {
return (
<nav>
<Link to="/contacts">All contacts</Link>
<Link to="/contacts/new">New contact</Link>
</nav>
);
}Because Link renders a real anchor, it still works with middle-click, "open in new tab", and crawlers. If JavaScript has not loaded yet, the browser follows the href as a normal navigation.
Highlighting the current page with NavLink
NavLink is a Link that knows whether its target matches the current URL. Use it for menus and sidebars. It adds an active class when the link matches. It adds a pending class while the next page's loader is running. It also adds a transitioning class during view transitions, if you enable them.
import { NavLink, Outlet } from "react-router";
export default function ContactsLayout() {
return (
<div>
<nav>
<NavLink to="/contacts" end>
All contacts
</NavLink>
<NavLink to="/contacts/new">New contact</NavLink>
</nav>
<Outlet />
</div>
);
}The browser also gets aria-current="page" on an active NavLink, which helps screen readers.
The end prop
By default a NavLink is active for its path and every path beneath it. /contacts is therefore active on /contacts/new and /contacts/42. Add end to match only the exact path. Without end, a link to / would be active on every page.
Styling with a function
className, style, and children each accept a function. It receives isActive, isPending, and isTransitioning.
<NavLink
to="/contacts"
className={({ isActive, isPending }) =>
isPending ? "pending" : isActive ? "active" : ""
}
>
Contacts
</NavLink>Common errors
- Using
<a>for internal links. It works but reloads the page. UseLinkfor paths inside the app and<a>for external sites. - Forgetting
end. The parent link stays highlighted on every child page. - Relative paths from the wrong place. A
towithout a leading slash resolves relative to the route that renders the link, not the full URL. Useto="42"inside a route for/contactsto reach/contacts/42. Use a leading slash when you want an absolute path. - Importing from the wrong package. In v7, import from
react-router, notreact-router-dom.
Exercise
A sidebar contains <NavLink to="/">Home</NavLink>. It is highlighted on every page. What fixes this, and why does it happen?
Show answer
Add the end prop: <NavLink to="/" end>. Without end, a NavLink is active for its path and all paths below it, and every URL starts with /. With end, it is active only when the URL is exactly /.
Exercise
Which NavLink class name appears while the next route's loader is still running?
Show answer
pending. The active class marks the link that matches the current location, and pending marks the link whose destination is currently loading.