Link and NavLink in React Router
Link and NavLink are React Router components used to navigate between different pages of a React application. The Link component provides client-side navigation, while NavLink provides the same navigation functionality along with support for identifying and styling the currently active page.
Course: React JS Training | JustAcademy React JS Training
1. What Is Link in React?
Link is a component provided by React Router that allows users to navigate to another URL within a React application without triggering a normal full-page reload during standard client-side navigation.
It is commonly used for navigation menus, buttons styled as links, product details, login pages, course pages, and other internal routes.
Example
import { Link } from "react-router";
function Home() {
return (
<div>
<h1>Home Page</h1>
<Link to="/about">Go to About Page</Link>
</div>
);
}
export default Home;
Explanation:
Link is imported from React Router.
to="/about" specifies the destination URL.
Go to About Page is the text displayed to the user.
- When the user clicks the link, React Router navigates to the matching route.
2. What Is NavLink in React?
NavLink is a specialized version of Link that provides information about whether the link is currently active. It is particularly useful for navigation menus, sidebars, tabs, and dashboards.
When a NavLink matches the current location, React Router automatically applies an active CSS class and sets aria-current="page". You can use this information to highlight the current page.
Example
import { NavLink } from "react-router";
function Navigation() {
return (
<nav>
<NavLink to="/" end>Home</NavLink>
<NavLink to="/about">About</NavLink>
<NavLink to="/contact">Contact</NavLink>
</nav>
);
}
export default Navigation;
Explanation:
NavLink creates a navigable link.
to specifies the URL destination.
- The active link can be styled differently from other links.
- The
end prop on the Home link ensures that it is active only at the root URL.
3. Why Do We Use Link and NavLink?
- Easy navigation: Allow users to move between pages.
- Single-page application experience: Navigate without a full-page reload during normal client-side navigation.
- Readable URLs: Use paths such as
/about and /courses.
- Better navigation menus: Highlight the current page using
NavLink.
- Accessibility: Preserve familiar link behavior, including keyboard navigation and browser link interactions.
- Reusable components: Build navigation menus that can be shared across application pages.
4. Install React Router
Before using Link and NavLink, install React Router in your React project if it is not already installed.
npm install react-router
The examples in these notes use the current declarative React Router imports from react-router. Existing projects and older tutorials may use react-router-dom, depending on the React Router version and project setup.
5. Set Up BrowserRouter
Link and NavLink need to be rendered inside a router context. In a typical browser application, you can wrap the root component with BrowserRouter.
import React from "react";
import ReactDOM from "react-dom/client";
import { BrowserRouter } from "react-router";
import App from "./App";
ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
BrowserRouter provides the routing context required for navigation components to work with the browser URL.
6. Using Link with Routes
To make a link display the correct page, define a corresponding route using Routes and Route.
import { Routes, Route, Link } from "react-router";
function Home() {
return <h2>Welcome to Home</h2>;
}
function About() {
return <h2>About Our Website</h2>;
}
function Contact() {
return <h2>Contact Us</h2>;
}
function App() {
return (
<div>
<nav>
<Link to="/">Home</Link> |{" "}
<Link to="/about">About</Link> |{" "}
<Link to="/contact">Contact</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</div>
);
}
export default App;
How it works: The navigation links specify destination paths, and the route definitions determine which component is rendered for each path.
7. Difference Between Link and NavLink
| Feature |
Link |
NavLink |
| Purpose |
Provides navigation between routes. |
Provides navigation and active-state information. |
| Client-side navigation |
Yes, for standard internal navigation. |
Yes, for standard internal navigation. |
| Active class |
Does not automatically provide an active class. |
Automatically applies the active class when active. |
| Active-state styling |
Can be implemented manually if needed. |
Can be styled using className, style, or render props. |
| Accessibility state |
Uses standard link semantics. |
Automatically sets aria-current="page" when active. |
| Common use |
Content links, product links, and general navigation. |
Navigation bars, sidebars, and active tabs. |
8. Styling NavLink with CSS
React Router automatically applies the active class to an active NavLink. You can use CSS to highlight the current page.
Navigation.jsx
import { NavLink } from "react-router";
import "./Navigation.css";
function Navigation() {
return (
<nav className="navigation">
<NavLink to="/" end>Home</NavLink>
<NavLink to="/about">About</NavLink>
<NavLink to="/services">Services</NavLink>
<NavLink to="/contact">Contact</NavLink>
</nav>
);
}
export default Navigation;
Navigation.css
.navigation {
display: flex;
gap: 20px;
padding: 15px;
background: #f2f2f2;
}
.navigation a {
text-decoration: none;
color: #333;
padding: 8px 12px;
}
.navigation a.active {
color: #ffffff;
background: #2563eb;
border-radius: 5px;
}
When the user visits a route, the matching NavLink receives the active class and uses the corresponding CSS rules.
9. Conditional Styling with className
You can also use the className callback to choose a CSS class based on the active state.
import { NavLink } from "react-router";
function Navigation() {
return (
<nav>
<NavLink
to="/dashboard"
className={({ isActive }) =>
isActive ? "selected-link" : "normal-link"
}
>
Dashboard
</NavLink>
</nav>
);
}
export default Navigation;
Here, isActive is a Boolean value. If the link matches the current route, the selected-link class is applied; otherwise, normal-link is applied.
10. Conditional Styling with the style Prop
The style callback lets you change inline styles based on whether a link is active.
import { NavLink } from "react-router";
function Navigation() {
return (
<nav>
<NavLink
to="/courses"
style={({ isActive }) => ({
color: isActive ? "blue" : "black",
fontWeight: isActive ? "bold" : "normal"
})}
>
Courses
</NavLink>
</nav>
);
}
export default Navigation;
This example displays the Courses link in blue and bold when it is active.
11. Understanding the end Prop
The end prop changes how NavLink determines whether a link is active. Without it, a link can remain active when the current URL is a deeper path under the link's destination. With end, the link must match through the end of the destination path.
<NavLink to="/courses">Courses</NavLink>
<NavLink to="/courses" end>Courses Home</NavLink>
| NavLink |
Current URL |
Active? |
to="/courses" |
/courses |
Yes |
to="/courses" |
/courses/react |
Yes |
to="/courses" end |
/courses |
Yes |
to="/courses" end |
/courses/react |
No |
Note: The root path / is treated specially by React Router so that a root NavLink does not remain active on every other route.
12. Using NavLink for a Sidebar
A sidebar commonly uses NavLink to show users which section of the application they are viewing.
import { NavLink } from "react-router";
function Sidebar() {
return (
<aside>
<h2>Admin Panel</h2>
<nav>
<NavLink to="/dashboard">Dashboard</NavLink>
<NavLink to="/dashboard/users">Users</NavLink>
<NavLink to="/dashboard/settings">Settings</NavLink>
</nav>
</aside>
);
}
export default Sidebar;
This approach is useful for admin dashboards, student portals, e-commerce management panels, and account settings pages.
13. Link with Dynamic Routes
You can use Link to navigate to a route containing a dynamic parameter, such as a product ID.
import { Link, Routes, Route, useParams } from "react-router";
function Products() {
return (
<div>
<h2>Products</h2>
<Link to="/products/101">View Product 101</Link>
<br />
<Link to="/products/102">View Product 102</Link>
</div>
);
}
function ProductDetails() {
const { productId } = useParams();
return <h2>Product ID: {productId}</h2>;
}
function App() {
return (
<Routes>
<Route path="/products" element={<Products />} />
<Route
path="/products/:productId"
element={<ProductDetails />}
/>
</Routes>
);
}
export default App;
When the user selects a product, the URL includes its ID. The useParams hook reads that ID from the matching route.
14. Common Mistakes and Solutions
| Mistake |
Why It Happens |
Solution |
| Link does not work |
The component is outside the router context. |
Wrap the application in BrowserRouter. |
| Incorrect destination |
The value of the to prop does not match the intended route. |
Check the path and the corresponding route definition. |
| Active link is not highlighted |
The CSS selector is incorrect or the URL does not match. |
Inspect the active class and review the matching path. |
| Several links appear active |
A parent path matches a deeper URL. |
Use the end prop where exact end matching is required. |
| Full-page reload occurs |
A regular anchor is used for an internal route. |
Use Link or NavLink for internal navigation. |
| Navigation leads to a 404 after refresh |
The server does not support the required SPA fallback. |
Configure the production server to serve the application entry HTML for client-side routes. |
15. Best Practices
- Use
Link for normal navigation when active-state styling is unnecessary.
- Use
NavLink for navigation menus, sidebars, and tabs that need an active state.
- Use meaningful URL paths such as
/courses and /about.
- Use the
end prop when a navigation item should be active only at the end of its target path.
- Keep navigation components reusable and easy to maintain.
- Use CSS classes for consistent active-state styling across the application.
- Keep link text descriptive and accessible.
- Use the correct router context and verify that each destination has a matching route.
16. Practical Project: Course Website Navigation
Objective: Create a course website with Home, Courses, About, and Contact pages. Use Link for simple navigation and NavLink for a navigation bar that highlights the active page.
Requirements
- Create a React project and install React Router.
- Wrap the app in
BrowserRouter.
- Create Home, Courses, About, and Contact components.
- Configure the paths with
Routes and Route.
- Use
NavLink for the main navigation menu.
- Use
Link for a call-to-action such as View Courses.
- Add CSS to highlight the active navigation item.
- Test each route and the browser Back and Forward buttons.
Sample Navigation Component
import { NavLink, Link } from "react-router";
function CourseNavigation() {
return (
<header>
<nav>
<NavLink to="/" end>Home</NavLink>
<NavLink to="/courses">Courses</NavLink>
<NavLink to="/about">About</NavLink>
<NavLink to="/contact">Contact</NavLink>
</nav>
<Link to="/courses">Explore Courses</Link>
</header>
);
}
export default CourseNavigation;
17. Interview Questions and Answers
Q1. What is Link in React Router?
Answer: Link is a component that enables client-side navigation to a specified URL in a React application.
Q2. What is NavLink?
Answer: NavLink is a specialized link component that provides active-state information for styling and accessibility.
Q3. What is the main difference between Link and NavLink?
Answer: Both support navigation, but NavLink additionally provides information about whether its destination is active.
Q4. What does the to prop do?
Answer: The to prop specifies the destination URL for a Link or NavLink.
Q5. How do you highlight the active navigation item?
Answer: Use NavLink and style its automatically applied active class, or use the className callback with isActive.
Q6. What is the purpose of the end prop?
Answer: It changes active matching so that the link is active only when the current URL matches through the end of its target path.
Q7. Can Link navigate to a dynamic route?
Answer: Yes. For example, <Link to="/products/101">View Product</Link> can navigate to a route such as /products/:productId.
Q8. Do Link and NavLink require a router?
Answer: In a typical browser application, they need to be rendered within a suitable React Router context, commonly provided by BrowserRouter.
18. Learning Checklist
- Understand the purpose of
Link.
- Understand the purpose of
NavLink.
- Know how to use the
to prop.
- Set up
BrowserRouter.
- Connect links to route definitions.
- Style active links with CSS.
- Use
className callbacks and the end prop.
- Build a navigation bar and sidebar.
- Test links, dynamic routes, and browser navigation.
19. Conclusion
Link and NavLink are important React Router components for creating smooth and accessible navigation. Use Link for general navigation and NavLink when you need to indicate which page is active. Together with BrowserRouter, Routes, and Route, they help create well-organized React applications.
Explore the course at JustAcademy React JS Training or Register for React JS Course Demo.
20. Official References