<NavLink>

A <NavLink> is a special kind of that knows whether or not it is “active” or “pending”. This is useful when building a navigation menu, such as a breadcrumb or a set of tabs where you’d like to show which of them is currently selected. It also provides useful context for assistive technology like screen readers.

  1. import { NavLink } from "@remix-run/react";
  2. <NavLink
  3. to="/messages"
  4. className={({ isActive, isPending }) =>
  5. isPending ? "pending" : isActive ? "active" : ""
  6. }
  7. >
  8. Messages
  9. </NavLink>;

Default active class

By default, an active class is added to a <NavLink> component when it is active so you can use CSS to style it.

  1. <nav id="sidebar">
  2. <NavLink to="/messages" />
  3. </nav>
  1. #sidebar a.active {
  2. color: red;
  3. }

className

The className prop works like a normal className, but you can also pass it a function to customize the classNames applied based on the active and pending state of the link.

  1. <NavLink
  2. to="/messages"
  3. className={({ isActive, isPending }) =>
  4. isPending ? "pending" : isActive ? "active" : ""
  5. }
  6. >
  7. Messages
  8. </NavLink>

style

The style prop works like a normal style prop, but you can also pass it a function to customize the styles applied based on the active and pending state of the link.

  1. <NavLink
  2. to="/messages"
  3. style={({ isActive, isPending }) => {
  4. return {
  5. fontWeight: isActive ? "bold" : "",
  6. color: isPending ? "red" : "black",
  7. };
  8. }}
  9. >
  10. Messages
  11. </NavLink>

children

You can pass a render prop as children to customize the content of the <NavLink> based on the active and pending state, which is useful to change styles on internal elements.

  1. <NavLink to="/tasks">
  2. {({ isActive, isPending }) => (
  3. <span className={isActive ? "active" : ""}>Tasks</span>
  4. )}
  5. </NavLink>

end

The end prop changes the matching logic for the active and pending states to only match to the “end” of the NavLinks’s to path. If the URL is longer than to, it will no longer be considered active.

Without the end prop, this link is always active because every URL matches /.

  1. <NavLink to="/">Home</NavLink>

To match the URL “to the end” of to, use end:

  1. <NavLink to="/" end>
  2. Home
  3. </NavLink>

Now this link will only be active at "/". This works for paths with more segments as well:

LinkURLisActive
<NavLink to=”/tasks” />/taskstrue
<NavLink to=”/tasks” />/tasks/123true
<NavLink to=”/tasks” end />/taskstrue
<NavLink to=”/tasks” end />/tasks/123false

caseSensitive

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

LinkURLisActive
<NavLink to=”/SpOnGe-bOB” />/sponge-bobtrue
<NavLink to=”/SpOnGe-bOB” caseSensitive />/sponge-bobfalse

aria-current

When a NavLink is active it will automatically apply <a aria-current="page"> to the underlying anchor tag. See aria-current on MDN.