Open In App

Difference Between NavLink an& Link

The Link and NavLink components are both provided by the React Router library and are used to create links in React applications. Link and NavLink are two main components in the React Router library, whose purpose is to create links in our application. The main difference between them is that Link provides a basic link to any URL, while NavLink offers additional capabilities that can enhance your user experience.

Link is the most basic component that React Router gives us to create links. When we use Link, we can think of it as the React version of the <a> tag in HTML, which allows us to create links to other pages.

Syntax:

<Link to="/path/to/link">Link text</Link>

Parameters:

Example: the following code would render a link to the /about page:

<Link to="/about">About</Link>
const userId = 123;
<Link to={`/users/${userId}`}>User {userId}</Link>
const handleClick = () => {
navigate("/about");
};
<button onClick={handleClick}>About</button>

Features:

The Link component in React Router is a declarative way to navigate between routes. It is similar to the anchor tag in HTML, but it has some additional features that make it more suitable for single-page applications.

Here are some of the features of the Link component:

Examples: To demonstrate the usage of the Link component in a React application.

import React from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';

const Home = () => <h2>Home Page</h2>;
const About = () => <h2>About Page</h2>;
const Contact = () => <h2>Contact Page</h2>;

const App = () => (
<Router>
    <div>
        <nav>
            <ul>
                <li>
                    <Link to="/">Home</Link>
                </li>
                <li>
                    <Link to="/about">About</Link>
                </li>
                <li>
                    <Link to="/contact">Contact</Link>
                </li>
            </ul>
        </nav>
        <Route path="/" exact component={Home} />
        <Route path="/about" component={About} />
        <Route path="/contact" component={Contact} />
    </div>
</Router>
);

export default App;

Output:

NavLink is another component that allows us to create links, but with additional capabilities. For example, with NavLink, we have the ability to know whether our link is in an "active" or "pending" state. We can adapt these states to our needs in order to display the link status differently. For example, we can change our CSS styles according to the link status.

Syntax:

<NavLink to="/" activeClassName="active">Home</NavLink>

This code will add the CSS class "active" to the Home link when it is active.

<NavLink to="/" activeStyle={{ color: "red" }}>Home</NavLink>

This code will make the Home link red when it is active.

Features

Here are some of the features of NavLink:

Examples: To demonstrate the usage of the NavLink component in a React application.

import React from 'react';
import { BrowserRouter as Router, Route, NavLink } from 'react-router-dom';

// Components for different pages
const Home = () => <h2>Home Page</h2>;
const About = () => <h2>About Page</h2>;
const Contact = () => <h2>Contact Page</h2>;

// App component
const App = () => (
<Router>
    <div>
        {/* Navigation */}
        <nav>
            <ul>
                {/* NavLink components for navigation links */}
                <li>
                    <NavLink exact to="/">Home</NavLink>
                </li>
                <li>
                    <NavLink to="/about">About</NavLink>
                </li>
                <li>
                    <NavLink to="/contact">Contact</NavLink>
                </li>
            </ul>
        </nav>

        {/* Route definitions */}
        <Route path="/" exact component={Home} />
        <Route path="/about" component={About} />
        <Route path="/contact" component={Contact} />
    </div>
</Router>
);

export default App;

Output:

Difference between Link and NavLink in React:

Features

Link

NavLink

Porpose

Creates a link to a different route in the application

Creates a link to a different route in the application and provides additional styling options

HTML element rendered

<a>

<a>

Additional props

None

activeClassName, activeStyle, isActive, exact

Use cases

For basic navigation links

For navigation links that need to be styled differently based on their active state

Active Link Behavior

All links behave the same regardless of their activity.

Allows for styling and behavior changes based on the active link.

Example

`<Link to="/about">About</Link>`

`<NavLink to="/about" activeClassName="active">About</NavLink>`

Summary

In the end, both Link and NavLink allow us to create links to different pages in our application, but NavLink offers us more advanced capabilities, such as the ability to display different styles for the link depending on whether we are on its path or not. The use of each component depends on your needs: if you need a simple link, Link will be sufficient. If you need more control over the appearance of the link, NavLink will be the right choice.

Article Tags :