
When building a React application, you often need more than a few independent pages. Imagine developing an admin dashboard with separate pages for users, products, orders, and settings.
You could create a separate route for every page, but what happens when multiple pages share the same layout, sidebar, or navigation menu?
This is where nested routes in React Router become useful.
Nested routes allow you to organize related pages under a parent route while sharing layouts and UI components. Instead of repeating the same sidebar and header on every page, you can define them once in a parent component and render different child pages inside it.
In this guide, you'll learn:
What nested routes are in React Router.
How parent and child routes work.
How to use the
Outletcomponent.How to create nested layouts.
How to handle dynamic routes and URL parameters.
How to create index routes and fallback pages.
How to implement protected nested routes.
Common mistakes and best practices.
By the end, you'll understand how to build a clean, maintainable routing structure for real-world React applications.
What Are Nested Routes in React Router?
Nested routes are routes defined inside another route.
A parent route can provide a shared layout, while its child routes render different content within that layout.
For example, consider an admin dashboard with the following URLs:
/admin
/admin/users
/admin/products
/admin/settingsAll these pages belong to the admin section and may share the same sidebar, header, and navigation.
Without nested routes, you might repeat the same layout in multiple components.
With nested routes, you can define a parent route for /admin and child routes for users, products, and settings.
A simplified route structure looks like this:
AdminLayout
├── Dashboard
├── Users
├── Products
└── SettingsThe parent component provides the common layout, while the child components display the page-specific content.
React Router supports this approach through nested route definitions and the Outlet component.
Why Do We Need Nested Routes?
Nested routes help solve several common problems in React applications.
1. Reuse Layouts
Many pages share common UI elements, such as:
Navigation bars
Sidebars
Headers
Footers
Breadcrumbs
Instead of repeating these components across multiple pages, you can place them in a parent layout.
2. Organize Related Pages
Nested routes help organize pages according to application features.
For example:
/dashboard
/dashboard/analytics
/dashboard/reports
/dashboard/settings
/profile
/profile/edit
/profile/securityThis makes the application's routing structure easier to understand.
3. Improve Maintainability
When multiple pages use the same layout, changes to the shared layout can be made in one place.
You don't need to update every child page individually.
4. Support Complex Applications
Nested routes are especially useful for applications such as:
Admin dashboards
E-commerce platforms
Learning management systems
Project management tools
Banking dashboards
SaaS applications
They allow developers to build complex navigation structures without making the routing configuration difficult to maintain.
Setting Up React Router
Before creating nested routes, let's set up a simple React application.
If you already have a React project, you can skip the project creation step.
Step 1: Create a React Application
You can create a React application using Vite:
npm create vite@latest nested-routes-demo -- --template reactNavigate into the project:
cd nested-routes-demoInstall the dependencies:
npm installStep 2: Install React Router
Install the React Router package:
npm install react-routerThis guide uses the current React Router declarative APIs from the react-router package. If your existing application uses React Router v6 or the react-router-dom package, check the version-specific documentation before adapting the imports.
Official documentation: https://reactrouter.com/
Understanding the Basic Routing Structure
Before moving to nested routes, let's quickly review how ordinary routes work.
Suppose your application has three pages:
Home
About
Contact
You can define routes using BrowserRouter, Routes, and Route.
Create the following components.
Home.jsx
function Home() {
return <h1>Home Page</h1>;
}
export default Home;About.jsx
function About() {
return <h1>About Page</h1>;
}
export default About;Contact.jsx
function Contact() {
return <h1>Contact Page</h1>;
}
export default Contact;Now configure your routes in App.jsx:
import {
BrowserRouter,
Routes,
Route,
Link
} from "react-router";
import Home from "./Home";
import About from "./About";
import Contact from "./Contact";
function App() {
return (
<BrowserRouter>
<nav>
<Link to="/">Home</Link>
{" | "}
<Link to="/about">About</Link>
{" | "}
<Link to="/contact">Contact</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</BrowserRouter>
);
}
export default App;Each route renders a different component when the URL matches.
However, this example doesn't demonstrate nested routes because each page is defined independently.
Let's improve it.
How to Create Nested Routes in React Router
Consider an application with an admin dashboard.
We want the following pages:
/admin
/admin/users
/admin/products
/admin/settingsEvery page should share the same admin layout.
We'll build this step by step.
Step 1: Create the Admin Layout
Create a file named AdminLayout.jsx.
import { Link, Outlet } from "react-router";
function AdminLayout() {
return (
<div>
<header>
<h1>Admin Dashboard</h1>
</header>
<nav>
<Link to="/admin">Dashboard</Link>
{" | "}
<Link to="/admin/users">Users</Link>
{" | "}
<Link to="/admin/products">Products</Link>
{" | "}
<Link to="/admin/settings">Settings</Link>
</nav>
<main>
<Outlet />
</main>
</div>
);
}
export default AdminLayout;Let's understand the important parts.
Link: Provides navigation between routes without requiring a full-page reload.
Outlet: Specifies where the matched child route should render.
The header and navigation remain visible while the content inside <Outlet /> changes.
This is the key concept behind nested routes.
Step 2: Create Child Components
Create Dashboard.jsx:
function Dashboard() {
return <h2>Welcome to the Admin Dashboard</h2>;
}
export default Dashboard;Create Users.jsx:
function Users() {
return <h2>Manage Users</h2>;
}
export default Users;Create Products.jsx:
function Products() {
return <h2>Manage Products</h2>;
}
export default Products;Create Settings.jsx:
function Settings() {
return <h2>Admin Settings</h2>;
}
export default Settings;Each component represents a separate page within the admin section.
Step 3: Configure Nested Routes
Now update App.jsx:
import {
BrowserRouter,
Routes,
Route
} from "react-router";
import AdminLayout from "./AdminLayout";
import Dashboard from "./Dashboard";
import Users from "./Users";
import Products from "./Products";
import Settings from "./Settings";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/admin" element={<AdminLayout />}>
<Route index element={<Dashboard />} />
<Route path="users" element={<Users />} />
<Route path="products" element={<Products />} />
<Route path="settings" element={<Settings />} />
</Route>
</Routes>
</BrowserRouter>
);
}
export default App;Notice how the child routes are defined inside the parent route.
The parent route renders AdminLayout, and its matching child route renders inside the layout's <Outlet />.
Also notice that the child paths don't start with /.
For example:
<Route path="users" element={<Users />} />Because users is a relative child path, React Router combines it with the parent path /admin.
The resulting URL is:
/admin/usersSimilarly:
Child Route | Final URL |
|---|---|
|
|
|
|
|
|
|
|
This approach keeps related routes grouped together.
What Is the Outlet Component?
The Outlet component is one of the most important parts of nested routing.
It acts as a placeholder where the matched child route is rendered.
Consider this example:
import { Outlet } from "react-router";
function DashboardLayout() {
return (
<div>
<header>
<h1>My Dashboard</h1>
</header>
<aside>
<p>Sidebar Navigation</p>
</aside>
<section>
<Outlet />
</section>
</div>
);
}When the user visits /dashboard/profile, React Router renders the matching profile component inside <Outlet />.
The layout remains in place, so the header and sidebar don't need to be recreated for every child page.
What Happens If You Don't Use Outlet?
Suppose your route configuration is correct, but your parent component doesn't contain <Outlet />.
function DashboardLayout() {
return (
<div>
<h1>Dashboard</h1>
</div>
);
}The parent layout may render, but its nested child route has no outlet in which to display its content.
Remember: Defining child routes is not enough. Your parent layout must render <Outlet /> wherever the child content should appear.
Understanding Index Routes
An index route is the default child route rendered when the parent route matches exactly.
For example:
<Route path="/admin" element={<AdminLayout />}>
<Route index element={<Dashboard />} />
<Route path="users" element={<Users />} />
</Route>When a user visits:
/adminReact Router renders the Dashboard component inside the admin layout.
When the user visits:
/admin/usersReact Router renders the Users component instead.
An index route is useful when you want the parent URL to display a default page without adding another path segment.
You should not assign a path to an index route.
Creating Multiple Levels of Nested Routes
Nested routes can contain other nested routes.
For example, imagine a project management application:
/projects
/projects/123
/projects/123/tasks
/projects/123/tasks/456You might want a project layout that contains a project overview, task list, and task details.
Here's an example:
import {
Routes,
Route
} from "react-router";
function ProjectsLayout() {
return (
<div>
<h1>Projects</h1>
<Outlet />
</div>
);
}
function ProjectDetailsLayout() {
return (
<div>
<h2>Project Details</h2>
<Outlet />
</div>
);
}
function ProjectOverview() {
return <p>Project overview</p>;
}
function TaskList() {
return <p>Project tasks</p>;
}
function TaskDetails() {
return <p>Task details</p>;
}You would import Outlet and define the routes like this:
import { Outlet } from "react-router";
<Routes>
<Route path="/projects" element={<ProjectsLayout />}>
<Route path=":projectId" element={<ProjectDetailsLayout />}>
<Route index element={<ProjectOverview />} />
<Route path="tasks" element={<TaskList />} />
<Route path="tasks/:taskId" element={<TaskDetails />} />
</Route>
</Route>
</Routes>This creates a hierarchy in which project-specific pages are nested inside the project layout.
For example, /projects/123/tasks/456 matches the project route, the project details layout, and the task details route.
Note: The first code snippet illustrates the layout components. In a working file, keep the imports at the top and combine the component definitions and route configuration into the same module or separate files as appropriate.
Understanding Dynamic Routes in Nested Routing
Dynamic routes allow a URL segment to represent a changing value.
For example:
/users/101
/users/102
/users/103Instead of defining a separate route for every user, you can use a dynamic parameter:
<Route path="users/:userId" element={<UserDetails />} />The :userId segment matches a value in the URL.
Inside the component, use useParams() to access the parameter.
import { useParams } from "react-router";
function UserDetails() {
const { userId } = useParams();
return (
<div>
<h2>User Details</h2>
<p>User ID: {userId}</p>
</div>
);
}
export default UserDetails;If the user visits /admin/users/101, the component receives "101" as the userId parameter, assuming the route is configured under /admin.
Dynamic parameters are strings, so convert them when your application needs a number.
For MongoDB applications, validate the parameter before querying the database.
Protected Nested Routes
In many applications, some pages should only be available to authenticated users.
For example, an admin dashboard may contain pages that require a signed-in user.
You can use a parent route to check authentication and render an outlet only when the user is authorized.
Here's a simplified example:
import {
Navigate,
Outlet
} from "react-router";
function ProtectedRoute({ isAuthenticated }) {
if (!isAuthenticated) {
return <Navigate to="/login" replace />;
}
return <Outlet />;
}
export default ProtectedRoute;You can use it in your route configuration:
<Routes>
<Route path="/login" element={<Login />} />
<Route element={<ProtectedRoute isAuthenticated={isAuthenticated} />}>
<Route path="/admin" element={<AdminLayout />}>
<Route index element={<Dashboard />} />
<Route path="users" element={<Users />} />
<Route path="settings" element={<Settings />} />
</Route>
</Route>
</Routes>Here, the protected parent route controls access to the nested admin section.
Important security note: Client-side route guards improve navigation and user experience, but they are not a security boundary. Your backend APIs must independently authenticate requests and enforce authorization rules. A user can bypass frontend navigation and call an API directly.
In real applications, authentication state should come from your application's authentication system rather than a hardcoded boolean.
Using Layout Routes Without Adding a URL Segment
Sometimes you need a shared layout but don't want an additional path segment in the URL.
React Router supports layout routes without a path.
For example:
<Route element={<MainLayout />}>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Route>The MainLayout component can contain a shared header, footer, and outlet.
import { Outlet } from "react-router";
function MainLayout() {
return (
<div>
<header>
<h1>My Website</h1>
</header>
<main>
<Outlet />
</main>
<footer>
<p>Copyright 2026</p>
</footer>
</div>
);
}The layout doesn't add another segment to the URL. Instead, it groups routes that share a common UI structure.
Handling a 404 Page with Nested Routes
A good application should display a useful message when a user visits an unknown URL.
You can define a wildcard route using *.
For example:
import { Routes, Route } from "react-router";
function NotFound() {
return <h2>404 - Page Not Found</h2>;
}
function App() {
return (
<Routes>
<Route path="/admin" element={<AdminLayout />}>
<Route index element={<Dashboard />} />
<Route path="users" element={<Users />} />
<Route path="products" element={<Products />} />
<Route path="*" element={<NotFound />} />
</Route>
<Route path="*" element={<NotFound />} />
</Routes>
);
}The nested wildcard route handles unknown child paths under /admin, while the top-level wildcard handles other unmatched URLs.
For example:
/admin/unknown-pagecan display the admin section's not-found page.
Similarly:
/unknown-pagecan display the application's general 404 page.
Common Mistakes When Using Nested Routes
Nested routes are easy to understand once you know the basic concepts, but a few mistakes can make them confusing.
1. Forgetting the Outlet Component
Problem: The parent layout renders, but the child page doesn't appear.
Solution: Add <Outlet /> to the parent component where the child content should render.
2. Using Incorrect Child Paths
Consider this configuration:
<Route path="/admin" element={<AdminLayout />}>
<Route path="users" element={<Users />} />
</Route>The child URL is /admin/users.
If you accidentally use an incorrect route structure, your intended hierarchy may not work as expected.
Use relative child paths when you want a child route to inherit its parent's path.
3. Confusing Index Routes with Path Routes
An index route renders at the parent route's URL.
Correct:
<Route index element={<Dashboard />} />A child route with path="dashboard" renders at a separate URL, such as /admin/dashboard.
Choose the approach that matches your desired URL structure.
4. Defining Routes Outside the Parent Route
If a page should share a parent's layout, define it as a child of that parent route.
Otherwise, it may render outside the intended layout.
5. Assuming Frontend Route Guards Provide Security
A protected React route does not prevent unauthorized API requests.
Always enforce authentication and permissions on the server.
6. Mixing Different React Router API Versions
React Router APIs and recommended imports can vary between versions.
Check your installed version and its documentation before copying examples from tutorials written for another version.
Best Practices for Nested Routes in React Router
Follow these best practices to keep your routing code clean and maintainable.
1. Keep related routes together.
Group routes by feature, such as admin, users, projects, or settings.
2. Use reusable layout components.
Create shared layouts for navigation, sidebars, headers, and footers.
3. Use Outlet correctly.
Place the outlet where the matched child content should appear.
4. Use index routes for default pages.
An index route is useful when the parent URL should display a default child page.
5. Keep route configuration readable.
For larger applications, consider organizing route components and configuration into feature-specific files.
6. Handle loading and error states.
Applications that fetch data should provide suitable loading, error, and empty states. React Router's data APIs can help manage these concerns when using a data router.
7. Validate dynamic route parameters.
Never assume that a URL parameter is valid just because React Router matched the route.
8. Protect backend resources.
Use server-side authorization for sensitive data and operations.
9. Test important navigation paths.
Test parent routes, child routes, unknown paths, direct URL navigation, and protected pages.
Frequently Asked Questions
1. What are nested routes in React Router?
Nested routes are routes defined inside a parent route. They allow child pages to share a parent layout while rendering different content for different URLs.
2. What is the purpose of Outlet in React Router?
Outlet specifies where the matched child route should render inside its parent component.
3. What is the difference between nested routes and normal routes?
Normal routes are defined independently. Nested routes are organized hierarchically, allowing child pages to share parent layouts and inherit URL structure.
4. What is an index route?
An index route is the default child route rendered when the parent route matches exactly. It doesn't require a separate path segment.
5. Can React Router support multiple levels of nested routes?
Yes. A child route can have its own child routes, allowing applications to create multiple levels of nested layouts and pages.
6. How do you access URL parameters in a nested route?
Use the useParams() hook to access dynamic URL parameters such as userId, projectId, or taskId.
7. Can nested routes have different layouts?
Yes. You can create separate parent layouts for different sections of an application and nest the relevant routes under each layout.
8. How do you protect nested routes?
You can create a parent route that checks authentication and renders <Outlet /> for authorized users or redirects unauthenticated users. Backend APIs must still enforce authentication and authorization.
9. Do nested routes cause full-page reloads?
Client-side navigation using React Router's Link or NavLink normally updates the route without a full-page reload. Direct navigation to a URL still requires the web server to serve the React application correctly.
10. When should you use nested routes?
Use nested routes when related pages share layouts, when URL paths represent a hierarchy, or when a large application needs a clear and maintainable routing structure.
Conclusion
Nested routes are an essential feature of React Router that help developers organize pages and build reusable layouts.
By defining parent and child routes, you can share common UI components while displaying different content based on the current URL.
The most important concepts to remember are:
Parent routes define shared layouts or route groups.
Child routes represent individual pages within those groups.
Outlet renders the matched child route.
Index routes provide default content for parent URLs.
Dynamic routes allow URL parameters to represent changing values.
Protected routes help manage frontend navigation for authenticated users.
Whether you're building a simple dashboard or a complex SaaS platform, nested routes can make your React application's routing structure easier to understand and maintain.
Start with a simple parent layout, add a few child routes, and expand the structure as your application grows.
Technical author and software engineer at DevByte. Writing deep-dive tutorials on distributed systems, modern full-stack web architectures, and cloud deployments.


