Link and NavLink components in React-Router-Dom
Last Updated :
17 Feb, 2025
When creating a React app, handling navigation properly is important. React Router provides two key components, Link and NavLink, to make moving between pages easy. These components work like regular <a> tags but prevent full-page reloads, making the app faster and smoother for users.
What is a Link Component?
In the React application, the Link component is used to navigate between pages without a full-page reload. It replaces the traditional <a> tag to improve performance. It is a simple navigator.
Syntax
<Link to="/path">Text</Link>
Features of Link
- Prevents Full Page Reload: Unlike traditional <a> tags, Link enables seamless navigation without refreshing the page.
- Uses the to Prop: Defines the destination URL.
- Supports Dynamic Routes: Can accept dynamic values for navigation.
Now let's understand the link component with an example
JavaScript
import React from "react";
import { Link, Route, Routes } from "react-router-dom";
function Home() {
return <h1>Home Page</h1>;
}
function About() {
return <h1>About Page</h1>;
}
function App() {
return (
<div>
<nav>
{/* Link works but does NOT add an active class */}
<Link to="/">Home</Link> | <Link to="/about">About</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</div>
);
}
export default App;
Output
Link Component in React-Router-DOMIn this example
- Uses the Link component from react-router-dom to navigate between Home and About pages without reloading the page.
- The Routes component contains Route elements that define the paths ("/" for Home and "/about" for About).
- Unlike NavLink, Link does not apply any special styling to indicate which page is active.
- Clicking "Home" or "About" updates the URL and displays the respective page, but the link itself does not highlight the active state.
What is NavLink Component?
The NavLink component extends Link by adding an "active" class styling. It is useful for highlighting active menu items in a navigation bar. It is used for highlighting the active path.
Syntax
<NavLink to="/path" activeClassName="active">Text</NavLink>
Features of NavLink
- Adds Active Class: Automatically applies an active class when the link matches the current route.
- Supports Custom Styling: Allows custom styles for active and inactive states.
- Exact Matching: By default, matches partially, but exact can be used for precise matching.
Now let's understand the NavLink component with an example
JavaScript
import React from "react";
import { NavLink, Route, Routes } from "react-router-dom";
function Home() {
return <h1>Home Page</h1>;
}
function About() {
return <h1>About Page</h1>;
}
function Navbar() {
return (
<nav>
{/* Using inline styles for active link */}
<NavLink
to="/"
style={({ isActive }) => ({
color: isActive ? "red" : "black",
fontWeight: isActive ? "bold" : "normal",
textDecoration: "none",
marginRight: "15px",
})}
>
Home
</NavLink>
<NavLink
to="/about"
style={({ isActive }) => ({
color: isActive ? "red" : "black",
fontWeight: isActive ? "bold" : "normal",
textDecoration: "none",
})}
>
About
</NavLink>
</nav>
);
}
function App() {
return (
<div>
<Navbar />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</div>
);
}
export default App;
Output
NavLinkIn this example
- The example uses NavLink instead of Link, which highlights the active page by applying different styles dynamically.
- When a link is active, it turns red and becomes bold, while inactive links remain black and normal.
- The navigation bar is placed in a separate Navbar component, making the code more modular and reusable.
- Clicking "Home" or "About" updates the URL and displays the respective page, while the active link gets highlighted automatically.
Link vs NavLink in React Router Dom
- Both Link and NavLink navigate without reloading the page.
- NavLink adds an "active" class to style the active page, while Link does not.
- Use NavLink for menus and navigation bars, and Link for normal navigation.
Feature | Link | NavLink |
---|
Purpose | Used for navigation between pages | Used for navigation with an active state styling |
---|
Active Styling | No built-in active class | Adds a special class to the active link |
---|
Used For | Normal links in the app | Navigation menus, tabs, or sidebars |
---|
Example | <Link to="/home">Home</Link> | <NavLink to="/home" className={({ isActive }) => isActive ? "active" : ""}>Home</NavLink> |
---|
Interesting Facts
Handling External Links
Since Link and NavLink are designed for internal routing, they cannot be used for external links. Instead, use a normal <a> tag.
<a href="https://example.com" target="_blank" rel="noopener noreferrer">
External Link
</a>
Combining useNavigate with Link
React Router also provides a useNavigate hook for programmatic navigation. If you need to navigate based on user actions (e.g., after form submission), use useNavigate instead of Link.
import { useNavigate } from "react-router-dom";
function Home() {
const navigate = useNavigate();
return (
<button onClick={() => navigate("/dashboard")}>
Go to Dashboard
</button>
);
}
Similar Reads
Link Component in React Router React Router is a powerful library in ReactJS that is used to create SPA (single-page applications) seamlessly. One of the components of the React Router is Link. In this article, we will be seeing the working of the Link component, various props, and usage of the Link component within React Router
5 min read
Difference between Link and Navigate Component in React Router In this article, we'll delve into the intricacies of two fundamental components within the widely acclaimed React Router Dom Library a Link and Navigate. As the backbone of many react applications, these components play pivotal roles in facilitating seamless navigation and routing. We'll explore the
4 min read
Navigate Component in React Router In React applications, navigation between different pages or views is an essential part of creating dynamic user interfaces. React Router is a popular library used for handling routing and navigation. One of the key features of React Router is the Navigate component, which allows for programmatic re
7 min read
Difference between NavLink and Link tag in React Router In React applications, efficient navigation is important for a seamless user experience. React Router, a popular library for handling routing in React applications, offers two primary components for navigation: NavLink and Link. Understanding the differences between these two components can help dev
4 min read
How to use Link Component in React JS? Material-UI provides a set of components that can be seamlessly integrated with React to create visually appealing and functional navigation elements. The Link component allows you to easily customize anchor elements with our own theme colors and typography styles. Material UI for React has this com
2 min read
Route Component in React Router React Router is a popular library used for managing routing in React applications. At its core lies the Route component, which plays a pivotal role in defining the relationship between URLs and corresponding components. In this article, we'll delve into the intricacies of the Route component, explor
5 min read
React MUI Navigation Components React Material-UI (MUI) is a popular library that provides a set of reusable components for building user interfaces in React applications. These components are based on Material Design, a design system developed by Google that provides guidelines for creating visually appealing, user-friendly inter
3 min read
How to use Portal Component in ReactJS ? The portal component renders its children into a new subtree outside the current DOM hierarchy. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Portal Component in ReactJS using the following approach.Prerequisites:NodeJS or NPMReact JSMate
2 min read
Explain the purpose of the Link component in React Router. React Router is a library in the React JS application. It is used to navigate the page to another page. It allows developers to create a seamless and dynamic user experience. It has many features the 'Link' Component stands out as a powerful tool for creating navigation links in React Applications.T
3 min read
How to open a component in a new tab in React JS ? React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL. In this tutorial, you will understand how to open a new component in another tab while
2 min read