Nested Routing — Complete Guide
Nested Routing — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.
On this page
React.js Tutorial · Lesson 32 of 100
Nested Routing
Beginner ✓ → Intermediate → Advanced → Professional
Intermediate · 2 — Build apps · ~14 min read · Module 4: Routing & Data
Introduction
You know the basics now. Here we use Nested Routing in real app situations — forms, pages, and data. Still plain language, just a bit more depth. Nested routing puts child routes inside a parent route. The parent shows a layout (sidebar, header) and an Outlet where the child page appears. Dashboard apps keep the same shell while only the inner content changes — faster and smoother than remounting the whole page.
Routing is how a single React app feels like many pages. Users expect URLs like /cart and /checkout — Router makes that work.
When will you use this?
Add routing when your app has more than one screen and URLs should be bookmarkable.
- Multi-page apps like dashboards use React Router — /settings, /profile, /orders.
- E-commerce sites route /product/123 to a product detail component.
Real-world: Zoho settings layout
Zoho admin keeps sidebar visible while /settings/profile and /settings/billing swap inner content — nested routes render inside a layout Outlet.
Production-style code
function SettingsLayout() {
return (
<div className="settings-shell">
<SettingsNav />
<main>
<Outlet /> {/* child route renders here */}
</main>
</div>
);
}
<Routes>
<Route path="/settings" element={<SettingsLayout />}>
<Route index element={<GeneralSettings />} />
<Route path="profile" element={<ProfileSettings />} />
<Route path="billing" element={<BillingSettings />} />
</Route>
</Routes>
What happens in production: Users never lose navigation context when switching settings tabs — standard pattern in Gmail, GitHub, and SaaS admin panels.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
import { Outlet } from 'react-router-dom';
function DashboardLayout() {
return (
<div>
<aside>Menu</aside>
<main><Outlet /></main>
</div>
);
}
// In Routes:
<Route path="/dashboard" element={<DashboardLayout />}>
<Route path="settings" element={<Settings />} />
<Route path="profile" element={<Profile />} />
</Route>
Line-by-line walkthrough
| Code | What it means |
|---|---|
import { Outlet } from 'react-router-dom'; | Imports code from React or another file so you can use it here. |
function DashboardLayout() { | Defines a function — often a component or event handler. |
return ( | Sends UI or a value back to whoever called this function. |
<div> | JSX tag — a UI element or custom component on the page. |
<aside>Menu</aside> | JSX tag — a UI element or custom component on the page. |
<main><Outlet /></main> | JSX tag — a UI element or custom component on the page. |
</div> | Part of the Nested Routing example — read it together with the lines before and after. |
); | Closes a block started by { or ( above. |
} | Closes a block started by { or ( above. |
// In Routes: | Comment — notes for humans; the computer ignores it. |
<Route path="/dashboard" element={<DashboardLayout />}> | React Router — maps URLs to page components. |
<Route path="settings" element={<Settings />} /> | React Router — maps URLs to page components. |
<Route path="profile" element={<Profile />} /> | React Router — maps URLs to page components. |
</Route> | React Router — maps URLs to page components. |
How it works (big picture)
- DashboardLayout wraps all /dashboard/* pages.
- Outlet is the placeholder for Settings or Profile.
- Paths are relative: settings not /dashboard/settings inside the nested Route.
Do this on your computer
- Create a layout component with
. - Nest Route elements inside the parent Route.
- Visit /dashboard/settings and confirm only the outlet area changes.
- Read the real-world section and name which part of the app uses this topic.
- Run the example locally and confirm the same behavior in the browser.
- Change one value in the example (text, initial state, or URL) and predict what will happen before you save.
Experiments — try changing this
- Change text or labels in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error message, then fix it.
- Open React DevTools (browser extension) while running Nested Routing and inspect component props/state.
Remember
Parent Route + layout + Outlet = nested routing. Child routes render inside Outlet. Good for admin panels and dashboards.
Common questions
What is Outlet?
A placeholder from React Router where the matched child route renders.
How long should I spend on Nested Routing?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.
What if I get stuck on Nested Routing?
Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.
Where is Nested Routing used in real jobs?
See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!