Community for developers to learn, share their programming knowledge. Register!
Routing with React Router

Implementing Link and NavLink Components in React


In this article, we’ll guide you through the process of implementing Link and NavLink components in React, essential tools for building dynamic, single-page applications with seamless navigation. If you’re looking to master these concepts, you can get training from this article, where we unpack the differences, use cases, and styling techniques for these two components. Whether you're an intermediate developer or a seasoned professional, understanding how to optimize navigation in your React projects is vital to providing a user-friendly experience.

React Router is an indispensable library for routing in React applications, allowing developers to create intuitive and efficient navigation systems. Two of the most widely used components in React Router are Link and NavLink. While they may seem similar at first glance, their differences and specific use cases can make or break the quality of your routing implementation. Let’s dive deeper into these components to understand their differences, how to use them, and how to style them effectively.

The Link and NavLink components are both used to create navigational links in React applications, but they serve different purposes and excel in distinct scenarios.

Link is the simpler of the two. It is designed to navigate users from one page to another within the application without reloading the page. In essence, it replaces the need for traditional HTML <a> tags in single-page applications. For example:

import { Link } from "react-router-dom";

function Home() {
  return (
    <div>
      <Link to="/about">Go to About Page</Link>
    </div>
  );
}

In this example, clicking the link will take the user to the /about route without refreshing the browser. This is key for maintaining a smooth user experience.

On the other hand, NavLink builds on the functionality of Link by adding styling capabilities, particularly for highlighting the active route. When a NavLink is used, it automatically applies an active class (or a custom class if specified) to the link corresponding to the current route. This makes it an excellent choice for navigation menus where you need to indicate the active page. Here’s an example:

import { NavLink } from "react-router-dom";

function Navbar() {
  return (
    <nav>
      <NavLink to="/" activeClassName="active-link" exact>
        Home
      </NavLink>
      <NavLink to="/about" activeClassName="active-link">
        About
      </NavLink>
    </nav>
  );
}

With this, the link for the current page will have the active-link class applied, making it easy to style active links differently.

To implement navigation links in your React application, you first need to install and configure React Router. If you haven’t already done so, install it using:

npm install react-router-dom

Once installed, wrap your application in a BrowserRouter component to enable routing:

import { BrowserRouter, Route, Routes } from "react-router-dom";
import Home from "./Home";
import About from "./About";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;

Here, the Routes component acts as a container for all the defined routes, and each Route maps a specific path to a component.

Now, you can use Link or NavLink to navigate between these pages. For instance:

import { Link } from "react-router-dom";

function Navbar() {
  return (
    <div>
      <Link to="/">Home</Link>
      <Link to="/about">About</Link>
    </div>
  );
}

This creates basic navigation between the Home and About pages. For a more interactive experience, use NavLink to highlight the active route:

import { NavLink } from "react-router-dom";

function Navbar() {
  return (
    <div>
      <NavLink to="/" exact activeClassName="active">
        Home
      </NavLink>
      <NavLink to="/about" activeClassName="active">
        About
      </NavLink>
    </div>
  );
}

One of the standout features of NavLink is its ability to style active links, making it clear to users which page they are currently on. By default, NavLink applies an active class to the active route. However, you can customize the class name using the activeClassName prop.

For instance, if you want the active link to appear bold and have a different color, you can define a CSS class like this:

.active {
  font-weight: bold;
  color: blue;
}

Then, use the NavLink component as follows:

import { NavLink } from "react-router-dom";

function Navbar() {
  return (
    <div>
      <NavLink to="/" activeClassName="active" exact>
        Home
      </NavLink>
      <NavLink to="/about" activeClassName="active">
        About
      </NavLink>
    </div>
  );
}

If you prefer using inline styles, you can use the style or isActive prop provided by NavLink. Here’s an example:

import { NavLink } from "react-router-dom";

function Navbar() {
  return (
    <div>
      <NavLink
        to="/"
        style={({ isActive }) => ({
          fontWeight: isActive ? "bold" : "normal",
          color: isActive ? "blue" : "black",
        })}
        exact
      >
        Home
      </NavLink>
      <NavLink
        to="/about"
        style={({ isActive }) => ({
          fontWeight: isActive ? "bold" : "normal",
          color: isActive ? "blue" : "black",
        })}
      >
        About
      </NavLink>
    </div>
  );
}

This approach gives you even more control over styling, as you can dynamically adjust styles based on the active route.

Summary

Routing is a fundamental aspect of any React application, and Link and NavLink components are indispensable tools for achieving seamless navigation. While Link provides a straightforward way to navigate between routes, NavLink offers additional styling capabilities that enhance user experience by highlighting the active route.

By understanding the differences between these two components, you can choose the right one for your specific use case. Additionally, leveraging the styling capabilities of NavLink ensures that your navigation menus are both functional and visually intuitive.

React Router continues to be the go-to solution for routing in React applications, and mastering its features, such as Link and NavLink, is essential for developing dynamic and user-friendly interfaces. For further details and advanced configurations, consider exploring the official React Router documentation.

With the knowledge gained from this article, you’re now well-equipped to implement effective navigation in your React projects.

Last Update: 24 Jan, 2025

Topics:
React