Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
Link and NavLink components in React-Router-Dom
Next article icon

Link and NavLink components in React-Router-Dom

Last Updated : 17 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Animationkk
Link Component in React-Router-DOM

In 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

Animationkk
NavLink

In 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>     ); } 

Next Article
Link and NavLink components in React-Router-Dom

T

tejaswaniagrawal23
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • ReactJS-Router

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences