Nested Routes in React
Learn how to create nested routes using React Router, display child pages inside a parent layout, use the Outlet component, and organize related pages with practical examples at CIIT Training Institute.
01 What Are Nested Routes?
Nested routes are routes defined inside another route. They are useful when multiple pages belong to one common section of an application and need to share the same layout.
For example, a student dashboard may contain Profile, Courses, and Results pages. All these pages belong to the Dashboard section. Instead of creating a separate dashboard layout for each page, we can create one parent route and define the related pages as its child routes.
In this structure, Dashboard is the parent route. Profile, Courses, and Results are child routes. When the user selects a child page, its content changes while the parent layout can remain visible.
02 Why Do We Need Nested Routes?
In real applications, several pages often share a header, sidebar, navigation menu, or other common UI elements. Nested routes help us keep these common elements in a parent component and display different child pages inside it.
- They organize related pages under a parent route.
- They allow multiple pages to reuse a common layout.
- They reduce repeated layout code.
- They create a clear URL hierarchy.
- They make routing easier to understand and maintain.
/dashboard
/dashboard/profile
/dashboard/courses
/dashboard/results
Student Dashboard
The dashboard heading and navigation are common elements. The selected child page appears below them.
03 Understanding the Outlet Component
React Router provides the Outlet component
to display the matched child route inside its parent route.
It acts as the location where the child component will be rendered.
Suppose Dashboard contains a heading and navigation menu. If the user opens the Courses page, the Dashboard component renders its common layout and Outlet displays the Courses component below the navigation menu.
import { Outlet, Link } from "react-router-dom";
function Dashboard() {
return (
<div>
<h1>Student Dashboard</h1>
<nav>
<Link to="profile">Profile</Link>
{" | "}
<Link to="courses">Courses</Link>
{" | "}
<Link to="results">Results</Link>
</nav>
<hr />
<Outlet />
</div>
);
}
export default Dashboard;
Explanation:
-
Outletis imported fromreact-router-dom. -
Linkcreates navigation links for the child pages. -
to="courses"navigates to the Courses child route relative to the current parent route. -
Outletdisplays whichever child route matches the current URL.
Outlet at the intended location.
04 Creating Child Route Components
Now create separate components for Profile, Courses, and Results. Each component contains the content of its own page. Keeping these components separate makes the application easier to organize and update.
function Profile() {
return (
<div>
<h2>Student Profile</h2>
<p>Welcome to your profile page.</p>
</div>
);
}
export default Profile;
Student Profile
Welcome to your profile page.
function Courses() {
return (
<div>
<h2>My Courses</h2>
<p>View your enrolled courses here.</p>
</div>
);
}
export default Courses;
My Courses
View your enrolled courses here.
function Results() {
return (
<div>
<h2>My Results</h2>
<p>View your examination results here.</p>
</div>
);
}
export default Results;
My Results
View your examination results here.
05 Configuring Nested Routes
After creating the parent and child components, configure them in the main application. Place the child Route elements inside the parent Route element.
import {
BrowserRouter,
Routes,
Route
} from "react-router-dom";
import Dashboard from "./Dashboard";
import Profile from "./Profile";
import Courses from "./Courses";
import Results from "./Results";
function App() {
return (
<BrowserRouter>
<Routes>
<Route
path="/dashboard"
element={<Dashboard />}
>
<Route
path="profile"
element={<Profile />}
/>
<Route
path="courses"
element={<Courses />}
/>
<Route
path="results"
element={<Results />}
/>
</Route>
</Routes>
</BrowserRouter>
);
}
export default App;
Step-by-step explanation:
- BrowserRouter: Enables browser-based routing for the application.
- Routes: Groups the route definitions.
-
Parent Route:
The path
/dashboardrenders the Dashboard component. -
Child Routes:
The relative paths
profile,courses, andresultsdefine the dashboard's child pages. - Outlet: Displays the matched child component inside Dashboard.
courses becomes
/dashboard/courses.
06 Understanding the URL and Rendering Flow
When the browser opens a nested URL, React Router matches both the parent route and the appropriate child route. The parent renders its layout, and the matching child appears through Outlet.
Student Dashboard
My Courses
View your enrolled courses here.
The dashboard heading and navigation belong to the parent component. The "My Courses" content belongs to the child Courses component. When the user navigates to another child URL, the matching child content changes.
07 Creating a Default Child Route
When a user visits /dashboard without selecting
a child page, we may want to display a welcome message.
React Router supports an index route for
this purpose.
An index route renders at the parent's URL. It does not add another segment to the URL.
<Route path="/dashboard" element={<Dashboard />}>
<Route
index
element={
<div>
<h2>Welcome to Dashboard</h2>
<p>Select a section to continue.</p>
</div>
}
/>
<Route path="profile" element={<Profile />} />
<Route path="courses" element={<Courses />} />
<Route path="results" element={<Results />} />
</Route>
Student Dashboard
Welcome to Dashboard
Select a section to continue.
<Outlet /> to display it.
08 Common Mistakes in Nested Routing
While creating nested routes, beginners can make a few common mistakes. Understanding them helps you troubleshoot routing problems more easily.
function Dashboard() {
return (
<div>
<h1>Student Dashboard</h1>
</div>
);
}
The parent component does not contain Outlet, so the child page will not appear inside this layout.
import { Outlet } from "react-router-dom";
function Dashboard() {
return (
<div>
<h1>Student Dashboard</h1>
<Outlet />
</div>
);
}
export default Dashboard;
Other mistakes to avoid:
- Defining child routes outside the intended parent route.
- Using a child URL that does not match the route configuration.
- Forgetting to import the required components.
- Forgetting to add an index route when default content is needed.
- Assuming Outlet automatically creates navigation links.
09 Practice Tasks
Complete the following tasks to practise nested routing using React Router.
Task 1: Create an Admin Dashboard
Create an AdminDashboard parent component with an Outlet. Add child routes for Users, Reports, and Settings.
Task 2: Create a Course Dashboard
Create a Courses parent page with child routes for Web Development, Data Analytics, and Software Testing.
Task 3: Add an Index Route
Display a welcome message when a user visits the parent dashboard URL without selecting a child page.
Task 4: Add Navigation Links
Use Link components to navigate between all child pages. Confirm that the parent layout remains visible while the child content changes.
Task 5: Test the URL Structure
Test the URLs for each child route and verify that the correct component is displayed through Outlet.
Summary
Nested routes allow related pages to share a common parent layout in React Router. Child Route elements are defined inside a parent Route, and the Outlet component displays the matching child content. Relative child paths create URLs such as /dashboard/profile and /dashboard/courses. An index route displays default content at the parent URL. Using nested routes helps organize application pages, reuse layouts, and maintain a clear routing structure.