--- title: NavLink --- # `` A `` is a special kind of `` 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][view-transitions] ```tsx import { NavLink } from "react-router-dom"; isPending ? "pending" : isActive ? "active" : "" } > Messages ; ``` ## Default `active` class By default, an `active` class is added to a `` component when it is active so you can use CSS to style it. ```tsx ``` ```css #sidebar a.active { color: red; } ``` ## `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. ```tsx [ isPending ? "pending" : "", isActive ? "active" : "", isTransitioning ? "transitioning" : "", ].join(" ") } > Messages ``` ## `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. ```tsx { return { fontWeight: isActive ? "bold" : "", color: isPending ? "red" : "black", viewTransitionName: isTransitioning ? "slide" : "", }; }} > Messages ``` ## `children` You can pass a render prop as children to customize the content of the `` based on the active and pending state, which is useful to change styles on internal elements. ```tsx {({ isActive, isPending, isTransitioning }) => ( Tasks )} ``` ## `end` 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. | Link | Current URL | isActive | | ------------------------------ | ------------ | -------- | | `` | `/tasks` | true | | `` | `/tasks/123` | true | | `` | `/tasks` | true | | `` | `/tasks/123` | false | | `` | `/tasks` | false | | `` | `/tasks/` | true | **A note on links to the root route** `` 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. ## `caseSensitive` Adding the `caseSensitive` prop changes the matching logic to make it case sensitive. | Link | URL | isActive | | -------------------------------------------- | ------------- | -------- | | `` | `/sponge-bob` | true | | `` | `/sponge-bob` | false | ## `aria-current` When a `NavLink` is active it will automatically apply `` to the underlying anchor tag. See [aria-current][aria-current] on MDN. ## `reloadDocument` The `reloadDocument` property can be used to skip client side routing and let the browser handle the transition normally (as if it were an ``). ## `viewTransition` The `viewTransition` prop enables a [View Transition][view-transitions] for this navigation by wrapping the final state update in `document.startViewTransition()`. By default, during the transition a `transitioning` class will be added to the `` element that you can use to customize the view transition. ```css a.transitioning p { view-transition-name: "image-title"; } a.transitioning img { view-transition-name: "image-expand"; } ``` ```jsx

Image Number {idx}

{`Img
``` You may also use the `className`/`style` props or the render props passed to `children` to further customize based on the `isTransitioning` value. ```jsx {({ isTransitioning }) => ( <>

Image Number {idx}

{`Img )}
``` [aria-current]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-current [view-transitions]: https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API