Active links with NavLink in React Router
NavLink is a Link that knows whether it points to the current location, so menus and tabs can highlight the page you are on.
How it works
- Use NavLink for menus. The tab bar renders
NavLinkinstead ofLink, because each tab needs to look different when it is the current page. [1] - Style the .active class. A small stylesheet created in code targets
.tabs a.active. Home and Inbox get the purple pill only through that class. [1] - Use the isActive callbacks. The Drafts tab passes a function as children to swap ○ for ●, and the Settings tab passes a function to
stylethat turns the text amber whenisActiveis true. [1]
Run and edit it
It runs in your browser, in a sandboxed frame that cannot see this site; the packages load from esm.sh only after you press Run.
Checked Oct 12, 2026: it starts without errors in Chromium with react-router-dom 7.18.4, react 19.3.0, react-dom 19.3.0. Needs WebGL.
More React Router examples
Basic routing in React Router Nested routes with Outlet in React Router URL params with useParams in React Router Programmatic navigation with useNavigate
Sources
Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.