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:
Routes Component in React Router
Next article icon

How to handle Nested Routes in React Router ?

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

React Router allows us to create a hierarchical structure where child components can be rendered within parent components, resulting in a seamless navigation flow. In this article, we will see how to handle nested Routes in React Router.

Approach to handle Nested Routes

To implement nested routes in React Router we define the children routes inside the Route component of the Parent route. In react-router-dom, we use Route with element and path attributes encapsulated in Routes. Use an Outlet component inside the parent route after the links of child components to show the link and switch between the child components rendering in Parent.

Steps to create the application:

Step 1: Create a React application by using the following command in the terminal.

npx create-react-app nesting-routes

Step 2: Now, go to the project folder i.e. nesting-demo by running the following command.

cd nesting-routes

Step 3: Let’s install the React Router DOM npm package required for this project

npm i react-router-dom

Project Structure:

Screenshot-2024-02-29-185800Dependencies:

"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.16.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}

Example: Here are four components among which Home will be the main page which will contain link to Profile component. The profile component further will nest Details and Followers. Cliking on any one of the link will change the URL and further the component will be displayed in the Outlet of the Profile component.

CSS
/* App.css */  .App {     text-align: center;     box-sizing: border-box;     min-height: 100vh; }  .homeDiv {     display: flex;     justify-content: center;     align-items: center;     height: 100vh; }  .profileBtn {     background-color: #1877f2;     color: white;     padding: 1rem;     border-radius: 0.4rem;     border: none;     font-size: 1.3rem;     text-decoration: none;     top: -50%;     left: -50%;     transform: translate(50%, 50%); }  .buttons {     margin-top: 2rem; }  .navBtn, .activeBtn {     padding: 1rem;     background-color: white;     color: black;     border-radius: 0.4rem;     box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;     margin: 2rem 1rem 2rem 1rem;     text-decoration: none;     font-size: 1.3rem; }  .activeBtn {     background-color: #1877f2;     color: white; }  .details {     padding: 1rem;     display: inline-block;     background-color: white;     color: black;     border-radius: 0.4rem;     border-color: 2px solid black;     box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;     margin: 5rem 1rem 2rem 1rem;     text-decoration: none;     font-size: 1rem; }  ul {     list-style: none; } 
JavaScript
//app.js  import { Routes, Route } from "react-router-dom"; import { Detail } from "./components/Detail"; import { Followers } from "./components/Followers"; import { Profile } from "./components/Profile"; import { Home } from "./components/Home"; import "./App.css";  function App() {     return (         <div className="App">             <Routes>                 <Route path="/" element={<Home />} />                 <Route path="/profile" element={<Profile />}>                     <Route path="/profile" element={<Detail />} />                     <Route path="/profile/followers" element={<Followers />} />                 </Route>             </Routes>         </div>     ); }  export default App; 
JavaScript
//Home.jsx  import { Link } from "react-router-dom";  export function Home() {     return (         <div className="homeDiv">             <Link to="/profile" className="profileBtn">                 Profile             </Link>         </div>     ); } 
JavaScript
//Profile.jsx  import { Link, Outlet, useLocation } from "react-router-dom";  export function Profile() {     const { pathname } = useLocation();      return (         <div>             <div className="buttons">                 <Link                     to="/profile"                     className={pathname ===                                      "/profile" ? "activeBtn" : "navBtn"}                 >                     Details                 </Link>                 <Link                     to="/profile/hobbies"                     className={pathname ===                                      "/profile/hobbies" ? "activeBtn" : "navBtn"}                 >                     Hobbies                 </Link>             </div>             <Outlet />         </div>     ); } 
JavaScript
//Details.jsx  import { Link } from 'react-router-dom'  export function Detail() {     return (         <div className="details">             Name: Bishal Paul             <br />             Social Media: @thebishalpaul             <br />             Country: India         </div>     ) } 
JavaScript
//Hobbies.jsx  export function Hobbies() {     return (         <ul className="details">             <li>Guitar</li>             <li>Coding</li>             <li>Reading Books</li>         </ul>     ) } 


Step to Run Application: Run the application using the following command from the root directory of the project.

npm start

Output:

nested


Next Article
Routes Component in React Router
author
bishalpaul34
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • ReactJS-Router

Similar Reads

  • Implement Nested Routes in React.js - React Router DOM V6
    Nested routes in React JS provide hierarchical navigation which is implemented using the outlet component in React Router Dom. Routing in React not only provides routing for the pages but also for rendering multiple components inside that page. Nested routes implement this by defining Routes for Chi
    4 min read
  • Explain Nested Routes in React
    Nested Routes are a type of routing in which one route is linked to another route. To set up routing on the client side in the React project and to enable navigation in our project, we will use the React Router library. React Router is a powerful library that helps in creating single-page applicatio
    5 min read
  • useRoutes Hook in React Router
    React Router is a library that is used for handling navigation and routing in React applications. It provides a way to navigate between different components or pages while maintaining a single-page application (SPA) structure. One of the key features is the useRoutes hook. It allows you to define ro
    4 min read
  • Routes Component in React Router
    Routes are an integral part of React Router, facilitating the navigation and rendering of components based on URL patterns. In this article, we'll delve into the concept of routes, understanding their role in React Router, and how to define and manage routes effectively. Table of Content What are Ro
    4 min read
  • How to Create a Protected Route with react-router-dom?
    A protected route in a React application is a route that only authorized users can access. This is useful for securing parts of an application that should not be available to everyone. We will see how we can create a protected route with react-router-dom. Prerequisites:NPM & NodeJSReactJSSteps t
    3 min read
  • What are the React Router hooks in React v5?
    React Router hooks perform client-side single-page routing that provides a functional and streamlined approach to managing navigation in React applications. It provides a way to directly access the state of the router from functional components, this simplifies tasks like retrieving information abou
    5 min read
  • React Router vs. React Router DOM
    Routing is a fundamental part of any web application. It allows users to move between pages or views. smoothly in traditional web development Routing involves mapping URLs to specific content or views on the server. These are the following topics that we are going to discuss: Table of Content What i
    4 min read
  • How To Handle Route Parameters in Express?
    Route parameters in ExpressJS capture dynamic values from URLs, like /users/:userId. These values are accessible in your route handler via req.params, enabling dynamic content generation. This allows for creating reusable routes that handle various inputs with a single pattern. [GFGTABS] JavaScript
    4 min read
  • How do you handle nested routes in Express.js?
    In this article we are going to learn that how can we setup nested routes in Express JS. Nested routes are the routes that are defined within other routes. This is used to organize your code. We are going to implement the nested routes using the below given two approaches. Table of Content Using Exp
    2 min read
  • How to Display a Simple Loading Indicator Between Routes in React Router ?
    Displaying a loading indicator between routes helps in transitioning between routes that involve loading data or components asynchronously. During this transition period, it's essential to provide visual feedback to users to indicate that something is happening. It is a good practice to display a lo
    3 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