Navigation with Link and NavLink
In this lesson, we will learn how to navigate between React routes using Link and NavLink, understand their differences, and create a navigation menu with active-link styling.
1. What Is Navigation in React?
Navigation means moving from one page or view to another within an application. For example, a user may move from the Home page to the Courses page or from the Courses page to the Contact page.
React Router provides the Link and NavLink components to support navigation between configured routes. They allow users to move between views without requiring a normal full-page navigation for each internal route change.
Remember: The destination URL must have a matching route definition for the appropriate page component to appear.
2. What Is the Link Component?
Link is a component provided by React Router. It creates a navigation link that changes the current route when the user clicks it.
The to prop specifies the destination path. The path should correspond to a route configured in the application.
Syntax
import { Link } from 'react-router-dom';
<Link to="/courses">
View Courses
</Link>
Explanation
- Link: The React Router navigation component.
- to: Specifies the destination URL path.
- View Courses: The text displayed to the user.
When the user clicks View Courses, React Router navigates to /courses and renders the matching route if one is configured.
3. Create a Navigation Menu Using Link
Consider a CIIT Training Institute website containing Home, About, Courses, and Contact pages. We can use Link to create a simple navigation menu.
import { Link } from 'react-router-dom';
function Navigation() {
return (
<nav>
<Link to="/">Home</Link>
{' | '}
<Link to="/about">About</Link>
{' | '}
<Link to="/courses">Courses</Link>
{' | '}
<Link to="/contact">Contact</Link>
</nav>
);
}
export default Navigation;
Expected Output
Home | About | Courses | Contact
The output displays four navigation links. Clicking each link changes the URL to its corresponding path.
Note: This code defines the navigation component. Render it inside your application to display the menu, and make sure all four paths have route definitions.
4. What Is the NavLink Component?
NavLink is similar to Link, but it also provides information about whether its destination matches the current route. This makes it useful for navigation menus where the active page should look different.
For example, when the user is on the Courses page, the Courses navigation link can appear blue and bold, while the other links retain their normal style.
Basic Syntax
import { NavLink } from 'react-router-dom';
<NavLink to="/courses">
Courses
</NavLink>
NavLink automatically supplies an isActive value to its function-based className or style callback. We can use that value to apply active styling.
5. Apply Active Styling with NavLink
We can use the className callback to assign a CSS class when a navigation link is active.
import { NavLink } from 'react-router-dom';
function Navigation() {
return (
<nav>
<NavLink
to="/"
className={({ isActive }) =>
isActive ? 'active-link' : 'nav-link'
}
>
Home
</NavLink>
<NavLink
to="/courses"
className={({ isActive }) =>
isActive ? 'active-link' : 'nav-link'
}
>
Courses
</NavLink>
</nav>
);
}
export default Navigation;
CSS Styling
.nav-link {
color: #374151;
text-decoration: none;
padding: 8px 12px;
}
.active-link {
color: #2563eb;
font-weight: bold;
text-decoration: underline;
}
How It Works
- NavLink checks whether its route is active.
- The callback receives the isActive value.
- If isActive is true, the active-link class is applied.
- Otherwise, the nav-link class is applied.
For the root path /, NavLink normally uses root matching behavior so it does not remain active for every route. You can explicitly use end when you want exact end matching.
6. Complete Example Using Link and NavLink
The following example demonstrates a navigation menu with active styling and multiple route components. It assumes React Router is installed and BrowserRouter is configured once around the application.
import {
Routes,
Route,
NavLink
} from 'react-router-dom';
function Home() {
return <h2>Welcome to CIIT Training Institute</h2>;
}
function Courses() {
return <h2>Our Training Courses</h2>;
}
function About() {
return <h2>About CIIT</h2>;
}
function App() {
return (
<div>
<h1>CIIT Training Institute</h1>
<nav>
<NavLink
to="/"
end
className={({ isActive }) =>
isActive ? 'active-link' : 'nav-link'
}
>
Home
</NavLink>
{' | '}
<NavLink
to="/courses"
className={({ isActive }) =>
isActive ? 'active-link' : 'nav-link'
}
>
Courses
</NavLink>
{' | '}
<NavLink
to="/about"
className={({ isActive }) =>
isActive ? 'active-link' : 'nav-link'
}
>
About
</NavLink>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/courses" element={<Courses />} />
<Route path="/about" element={<About />} />
</Routes>
</div>
);
}
export default App;
CSS for Active Navigation
.nav-link {
color: #374151;
text-decoration: none;
padding: 8px 12px;
}
.active-link {
color: #2563eb;
font-weight: bold;
text-decoration: underline;
}
Expected Behavior
- Opening / displays the Home page and activates Home.
- Opening /courses displays Courses and activates Courses.
- Opening /about displays About and activates About.
- Clicking a navigation link changes the displayed route.
7. Difference Between Link and NavLink
| Feature | Link | NavLink |
|---|---|---|
| Navigation | Supports navigation between routes. | Supports navigation between routes. |
| Active route information | Does not provide NavLink's isActive callback. | Provides isActive information. |
| Active styling | Must be managed separately if needed. | Can apply styling based on the active route. |
| Common use | Regular links within the application. | Navigation menus and active page indicators. |
Easy rule: Use Link for ordinary navigation. Use NavLink when you need active-route styling or active-state information.
8. Common Mistakes
Mistake 1: Using the wrong destination prop
// Incorrect
<Link href="/courses">Courses</Link>
// Correct
<Link to="/courses">Courses</Link>
React Router Link uses the to prop to specify the destination, not href.
Mistake 2: Missing route definition
If a link points to /courses but no matching route exists, the expected Courses component will not appear. Define the corresponding Route.
Mistake 3: Using Link outside the router
Components using Link or NavLink must be rendered within the configured router.
Mistake 4: Expecting automatic active styling from Link
Link does not automatically supply NavLink's isActive callback. Use NavLink when active-state styling is required.
Practice Tasks
- Create a navigation menu with Home, About, Courses, and Contact.
- Use Link to navigate between the configured routes.
- Replace the navigation links with NavLink.
- Apply a blue color to the active navigation link.
- Make the active link bold and underlined.
- Use the end prop on the Home NavLink.
- Explain the difference between Link and NavLink.
- Correct an example that incorrectly uses href on Link.
Summary
Link and NavLink are React Router components used to navigate between configured routes. Link provides ordinary client-side navigation through its to prop, while NavLink also exposes active-route information through isActive. This makes NavLink useful for highlighting the current page in navigation menus. Always configure matching routes and render navigation components inside the router. In the next lesson, we will learn programmatic navigation using the useNavigate hook.