Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
How to create a navigation bar using React-Bootstrap?
Next article icon

Create a basic Navbar using React Router v6

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

React Router is a library for handling routing and navigation in React JS Applications. It allows you to create dynamic routes, providing a seamless user experience by mapping various URLs to components. It enables navigation in a single-page application (SPA) without refreshing the entire page.

React Router recently released version 6, and earlier version 5 of React Router was used. A lot of changes have been made in v6. In this article, we will understand how to create a basic navbar using React Router v6.

Approach to Create a Basic Navbar using React Router v6

  • Use <Link> Instead of <a> to prevent full page reloads in React apps.
  • <Link> uses the to attribute instead of href for navigation
  • Define routes using createBrowserRouter for structured routing.
  • Wrap your app with RouterProvider and pass the router using the router prop.
  • Use <NavLink> instead of Link to identify the active route.
  • <NavLink> automatically adds an active class to the current Link.
  • Customize the appearance of the active link using the .active class in CSS.

Example: className={({ isActive }) => isActive ? 'active' : ''} for conditional styling.

Steps to Create a React App and Install React Router v6

Step 1: Create a React project folder, open the terminal, and write the following command.

bash
# Run this to use npm npx create-react-app foldername # Or run this to use yarn yarn create react-app foldername 


Step 2: Navigate to the root directory of your project using the following command.

bash
cd foldername 


Step 3: Now, install the React Router library

bash
npm install react-router-dom or yarn add react-router-dom 


Follow this Project Structure for React Router v6:

Screenshot30
Project Structure

The updated dependencies in package.json file will look like:

bash
"dependencies": {     "react": "^18.2.0",     "react-dom": "^18.2.0",     "react-scripts": "5.0.1",     "web-vitals": "^2.1.4",     "react-router-dom": "^6.22.0" } 

Syntax to setup Browser Router:

bash
import { createBrowserRouter } from "react-router-dom"; const router = createBrowserRouter([   {     path: "/"     element:    } ]); 

Example: Below is an example of creating a basic Navbar using React Router v6.

App.css
/* App.css */  * {     padding: 0;     margin: 0; } body {     font-family: system-ui; } nav {     padding: 10px;     margin-bottom: 10px;     background: green; } ul {     list-style-type: none; } li {     display: inline;     margin-right: 20px; } a {     text-decoration: none;     color: white; }  a:hover {     color: #eee; } 
NavBar.js
// NavBar.js  import { Link } from 'react-router-dom'; const Navbar = () => {     return (                 Home                 About                 Projects                 Contact     ); } export default Navbar; 
Index.js
// index.js import React from 'react' import ReactDOM from 'react-dom/client' import {     RouterProvider,     createBrowserRouter } from 'react-router-dom' import App from './App' import Home from './Home' import About from './About' import Projects from './Projects' import Contact from './Contact' const router = createBrowserRouter([     {         path: '/',         element: ,         children: [             {                 path: '/',                 element: ,             },             {                 path: '/about',                 element: ,             },             {                 path: '/projects',                 element: ,             },             {                 path: '/contact',                 element: ,             },         ]     } ]); ReactDOM.createRoot(document.getElementById('root')).render() 
App.js
// App.js import { Outlet } from "react-router-dom"; import "./App.css"; import Navbar from "./NavBar"; const App = () => {     return (         <> );         }; export default App; 
Home.js
// Home.js const Home = () => {     return Home; } export default Home; 
About.js
// About.js const About = () => {     return About; } export default About; 
Project.js
// Projects.js const Projects = () => {     return Projects; } export default Projects; 
Contact.js
// Contact.js const Contact = () => {     return Contact; } export default Contact; 

Start your application using the following command.

bash
npm start 


Output:

link-navbar
Basic NavBar

Example 2: Below is an example of creating a Navbar using React Router v6 and NavLink.

App.css
/* App.css */ * {   padding: 0;   margin: 0; } body {     font-family: system-ui; } nav {     padding: 10px;     margin-bottom: 10px;     background: green; } ul {     list-style-type: none; } li {     display: inline;     margin-right: 20px; } a {     text-decoration: none;     color: white; }  a:hover {     color: #eee; }  a.active {      text-decoration: underline;  } 
NavBar.js
// NavBar.js import { NavLink } from 'react-router-dom';   const Navbar = () => {     return (                 Home                 About                 Projects                 Contact     ); }  export default Navbar; 
Home.js
// Home.js const Home = () => {     return Home; } export default Home; 
About.js
// About.js const About = () => {     return About; } const Projects = () => {     return Projects; } const Contact = () => {     return Contact; } export default Contact; export default Projects; export default About; 

Output:

navlink
Using Navlink

createRoutesFromElements

If you do not like the syntax to create routes from these objects then we can also use element instead of objects. To do this, we will call another function in createBrowserRouter which is createRoutesFromElements and in this function, we will set up the routes.

Example: Below is an sample example of createRoutesFromElements.

App.jsx
import React from "react"; import ReactDOM from "react-dom/client"; import App from "./App"; import Home from "./Home"; import About from "./About"; import Projects from "./Projects"; import Contact from "./Contact"; import {     RouterProvider,     createBrowserRouter,     createRoutesFromElements,     Route, } from "react-router-dom";  const router = createBrowserRouter(     createRoutesFromElements(         }> } /> } /> } /> } /> ) ); ReactDOM.createRoot(document.getElementById("root")).render(); 

Output:

gfg66
Output



Next Article
How to create a navigation bar using React-Bootstrap?
author
tapeshdua420
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • ReactJS-Router

Similar Reads

  • Create a Responsive Navbar using ReactJS
    Creating a responsive navbar in ReactJS is essential for providing an optimized user experience across various screen sizes. A responsive navbar adapts its layout based on the screen size, ensuring easy navigation on both mobile and desktop devices. In this article, we will walk you through the step
    6 min read
  • How to create a navigation bar using React-Bootstrap?
    React-Bootstrap is a front-end framework that was designed with React in mind. The NavBar Component is a navigation header that is responsive and powerful. It supports navigation, branding, and many other related features. In this article, we are going to implement a navigation bar using React Boots
    2 min read
  • Create Navbars UI using React and Tailwind CSS
    A UI plays an important role because a clean, well-designed interface creates a positive first impression and if the UI is good then users can stay on our website some more time and if the UI is bad then they can not stay on our site for more time. we will see how to Create Navbars UI using React an
    5 min read
  • Create Web Browser using React-Native
    In this article, we will build the Web Browser using the React Native language. In this application, there will be as search bar with the button. The uer can enter the search text or the URL and click on the button, the browser will take to the desired result. Also there are morre naivtional buttons
    7 min read
  • Migrate to React Router v6
    React Router v6 brings significant changes and improvements over its predecessor, offering a more intuitive API and better performance. Migrating to React Router v6 might seem daunting, but with proper guidance, the transition can be smooth and beneficial for your React applications. Enhancements in
    3 min read
  • How to create Breadcrumb Navigation Using React ?
    Breadcrumbs are useful in navigation and provide users with links representing their path through a website or application. In this tutorial, we'll create a simple e-commerce website with product listings and detail pages, incorporating breadcrumbs for a better user experience. Output Preview: Let u
    6 min read
  • How to make rounded corner for navbar using react-bootstrap?
    In single-page applications, Navigation Bars are the most important components of web applications to provide national access to different sections and features of the applications. We can customize the navigation bar by adjusting the colors, width, and we can also make the rounded corners for the n
    3 min read
  • How to Add Icon in NavBar using React-Bootstrap ?
    This article will show you how to add an icon in the navigation bar using React-Bootstrap. We will use React-Bootstrap because it makes it easy to use reusable components without implementing them. PrerequisitesReactJSJSXReact BootstrapCreating React App and Installing ModuleStep 1: Create a React a
    2 min read
  • React Suite Navbar Appearance
    React suite is a library of React components that have a sensible UI design and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React Suite Navbar A
    3 min read
  • Create a Restaurant Website Template using React-Native
    The restaurant template app helps customers to explore, choose, and order their preferred dishes from restaurants. In this article, we will create a Tour and Travels website template using React Native. Preview of final output: Let us have a look at how the final output will look like. Prerequisite:
    4 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