NavLink NavLink Active Styling

Last updated: 2026-11-02

NavLink NavLink Active Styling

NavLink is a Link that knows about the active state: when the current address matches its to prop, it automatically adds an active class name to the rendered a tag, or calls your className function with the current state. Navigation bars and sidebars use it to highlight the page the visitor is on, which takes far less code than comparing the location by hand every time.

GroupRouting & State Mgmt
KindComponent
SinceReact Router 6.4

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

import { NavLink } from 'react-router-dom'

<NavLink
  to="/about"
  className={({ isActive }) => isActive ? 'nav-item active' : 'nav-item'
}>About</NavLink>

// Shorthand: adds class="active" by default on a match
<NavLink to="/about">About</NavLink>

⚙️ Parameters

toThe target path, e.g. /users/42; relative paths and the object form { pathname, search } work too.
classNameEither a string or a ({ isActive }) => string function that returns a class name based on the active state.

Returns:Nothing (it is a component). It renders a single a tag and carries the active styling while the current address matches to.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

Clicking an item turns it blue and bold and shows a check mark - the same effect as a real NavLink whose isActive drives the className function (simulated demo).

💡 Key Points

💡 Tip The end prop controls exact matching: with end on /users the link /users/42 is no longer highlighted, without it the match is a prefix match.
ℹ️ Info In v6 both className and children accept a function signature ({ isActive, isPending }) => ..., and v7 adds isTransitioning.
⚠️ Warning NavLink renders a real a tag; do not prevent the default behavior in onClick and navigate by hand - let the Router take over the navigation.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q What is the difference between NavLink and Link?
A They work the same way (both are a tags that navigate without a reload); the only difference is that NavLink exposes the active state isActive, which is what navigation bars need. Use Link for ordinary jumps.
Q How do I highlight /users only on an exact match?
A Add the end prop: <NavLink to="/users" end>, which turns off the default prefix matching.
Q Can I change the text when active, not only the class name?
A Yes, children accepts a function too: ({ isActive }) => isActive ? 'Current: About' : 'About'.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏