<NavLink>
A <NavLink>
is a special kind of <Link>
that knows whether or not it is "active", "pending", or "transitioning". This is useful in a few different scenarios:
- 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 provides useful context for assistive technology like screen readers
- It provides a "transitioning" value to give you finer-grained control over View Transitions
import { NavLink } from "react-router-dom";
<NavLink
to="/messages"
className={({ isActive, isPending }) =>
isPending ? "pending" : isActive ? "active" : ""
}
>
Messages
</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.
<nav id="sidebar">
<NavLink to="/messages" />
</nav>