Nested Routes and Not Found Pages in React JS
Nested Routes allow developers to organize related pages inside a parent route and share common layouts such as headers, sidebars, navigation menus, and footers. Not Found Pages, commonly known as 404 pages, display a helpful message when a user visits a URL that does not match any configured route.
These concepts are essential for developing structured, scalable, and user-friendly React applications using React Router. Learn more through JustAcademy React JS Training or Register for React JS Course Demo.
1. What Are Nested Routes?
Nested Routes are routes defined inside a parent route. The parent route usually provides a common layout, while its child routes display different pages within that layout.
For example, an admin dashboard may contain multiple sections, including Home, Users, Products, Settings, and Reports. Instead of repeating the dashboard layout on every page, developers can create one parent component and render different child components inside it.
Example URL Structure
/dashboard
/dashboard/users
/dashboard/products
/dashboard/settings
/dashboard/reports
Each URL represents a different page, but all pages can share the same dashboard header and navigation menu.
Real-World Applications
- E-commerce: Product categories, product details, cart, and checkout pages.
- Admin dashboards: Users, reports, settings, and analytics pages.
- Learning platforms: Courses, lessons, assignments, and student profiles.
- Banking applications: Accounts, transactions, statements, and settings.
- Social applications: User profiles, posts, followers, and account settings.
2. Why Do We Use Nested Routes?
- Reusable layouts: Display a shared header, sidebar, or footer across multiple pages.
- Less code duplication: Avoid writing the same layout repeatedly.
- Organized routing: Group related pages under a common parent path.
- Easy maintenance: Update a shared layout in one component.
- Scalability: Add new child pages without duplicating existing layouts.
- Better navigation: Create a clear and logical URL structure.
3. Understanding Parent Routes and Child Routes
A parent route contains one or more child routes. The parent component can provide a layout, and React Router renders the matched child component through the Outlet component.
| Concept |
Description |
Example |
| Parent route |
Defines a shared layout or route hierarchy. |
/dashboard |
| Child route |
Displays a specific page inside the parent layout. |
/dashboard/users |
| Outlet |
Provides the location where the matched child route renders. |
<Outlet /> |
| Index route |
Displays the default child page at the parent URL. |
/dashboard |
| Wildcard route |
Matches otherwise unmatched paths at its routing level. |
* |
4. What Is the Outlet Component?
Outlet is a React Router component that renders the matching child route inside its parent component. It works as a placeholder for nested page content.
If the parent component does not include an Outlet, the child route will not appear at the intended location within the parent layout.
Example: DashboardLayout.jsx
import { Link, Outlet } from "react-router";
function DashboardLayout() {
return (
<div>
<header>
<h1>Admin Dashboard</h1>
</header>
<nav>
<Link to="/dashboard">Home</Link> |
<Link to="/dashboard/users">Users</Link> |
<Link to="/dashboard/settings">Settings</Link>
</nav>
<main>
<Outlet />
</main>
</div>
);
}
export default DashboardLayout;
The header and navigation remain visible when users move between child pages. Only the content rendered by <Outlet /> changes.
5. Step-by-Step Implementation of Nested Routes
Step 1: Install React Router
If React Router is not installed, install the package appropriate for your project.
npm install react-router
Version note: Current React Router documentation supports imports from react-router. Many existing projects use react-router-dom. Check the installed version and use its supported import paths consistently.
Step 2: Create the Dashboard Home Component
Create a file named DashboardHome.jsx.
function DashboardHome() {
return (
<div>
<h2>Dashboard Home</h2>
<p>Welcome to the admin dashboard.</p>
</div>
);
}
export default DashboardHome;
Step 3: Create the Users Component
Create a file named Users.jsx.
function Users() {
return (
<div>
<h2>User Management</h2>
<p>Manage application users here.</p>
</div>
);
}
export default Users;
Step 4: Create the Settings Component
Create a file named Settings.jsx.
function Settings() {
return (
<div>
<h2>Settings
<p>Update your dashboard settings here.</p>
</div>
);
}
export default Settings;
Step 5: Configure the Nested Routes
In App.jsx, define the parent route and its child routes.
import { BrowserRouter, Routes, Route } from "react-router";
import DashboardLayout from "./DashboardLayout";
import DashboardHome from "./DashboardHome";
import Users from "./Users";
import Settings from "./Settings";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/dashboard" element={<DashboardLayout />}>
<Route index element={<DashboardHome />} />
<Route path="users" element={<Users />} />
<Route path="settings" element={<Settings />} />
</Route>
</Routes>
</BrowserRouter>
);
}
export default App;
Step 6: Test the Routes
| URL |
Expected Output |
/dashboard |
Dashboard Home inside the dashboard layout. |
/dashboard/users |
User Management inside the dashboard layout. |
/dashboard/settings |
Settings inside the dashboard layout. |
6. Understanding Index Routes
An index route is the default child route displayed when a user visits the parent route directly. It does not add another URL segment.
Example
<Route path="/dashboard" element={<DashboardLayout />}>
<Route index element={<DashboardHome />} />
<Route path="users" element={<Users />} />
</Route>
When the user visits /dashboard, the Dashboard Home component renders inside the parent's Outlet. When the user visits /dashboard/users, the Users component renders instead.
Index routes are useful when a parent page should display meaningful default content rather than an empty content area.
7. What Are Not Found Pages?
A Not Found Page is a page displayed when the requested URL does not match any available route. It is commonly known as a 404 page.
For example, an application may define routes for /, /about, and /contact. If a user visits /unknown-page, the application can display a Not Found Page.
Example URLs
/ - Home Page
/about - About Page
/contact - Contact Page
/unknown-page - Not Found Page
8. Why Are Not Found Pages Important?
- Better user experience: Clearly explain that the requested page is unavailable.
- Easy navigation: Provide a link to return to the home page.
- Professional design: Prevent users from seeing a blank page for invalid URLs.
- Helpful error handling: Guide users when they enter an incorrect URL.
- Consistent interface: Keep error pages visually consistent with the rest of the application.
9. Creating a Not Found Page in React Router
In a declarative React Router application, a wildcard route using path="*" can match paths that do not match other routes at the same routing level.
Step 1: Create NotFound.jsx
import { Link } from "react-router";
function NotFound() {
return (
<div>
<h1>404 - Page Not Found</h1>
<p>Sorry, the page you are looking for does not exist.</p>
<Link to="/">Return to Home</Link>
</div>
);
}
export default NotFound;
Step 2: Add the Wildcard Route
import { BrowserRouter, Routes, Route } from "react-router";
import Home from "./Home";
import About from "./About";
import NotFound from "./NotFound";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<NotFound />} />
</Routes>
</BrowserRouter>
);
}
export default App;
Step 3: Test the Not Found Page
- Visit
/ to display the Home page.
- Visit
/about to display the About page.
- Visit
/this-page-does-not-exist to display the Not Found page.
Important: A client-side wildcard route renders a Not Found interface, but it does not automatically guarantee that the server returns an actual HTTP 404 status code. Server or framework-level handling may be required when the correct HTTP status is important.
10. Combining Nested Routes and Not Found Pages
Nested routes and Not Found Pages can work together in the same application. A dashboard can have its own child routes, while wildcard routes handle invalid paths within the dashboard and elsewhere in the application.
Example Route Configuration
import { BrowserRouter, Routes, Route } from "react-router";
import DashboardLayout from "./DashboardLayout";
import DashboardHome from "./DashboardHome";
import Users from "./Users";
import Settings from "./Settings";
import NotFound from "./NotFound";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/dashboard" element={<DashboardLayout />}>
<Route index element={<DashboardHome />} />
<Route path="users" element={<Users />} />
<Route path="settings" element={<Settings />} />
<Route path="*" element={<NotFound />} />
</Route>
<Route path="*" element={<NotFound />} />
</Routes>
</BrowserRouter>
);
}
export default App;
In this example, the nested wildcard route handles unmatched dashboard subpaths, while the top-level wildcard handles unmatched application paths.
| URL |
Expected Result |
/dashboard |
Dashboard Home. |
/dashboard/users |
User Management. |
/dashboard/unknown |
Not Found component inside the dashboard layout. |
/unknown |
Top-level Not Found component. |
11. Difference Between a Not Found Route and an Error Boundary
A Not Found route and an error boundary have different purposes. A wildcard route handles URLs that do not match configured routes. An error boundary handles supported errors that occur during route rendering or data processing.
| Feature |
Not Found Route |
Error Boundary |
| Purpose |
Handle unmatched URLs. |
Handle supported rendering or route data errors. |
| Example |
/invalid-page |
A route loader fails or throws an error. |
| Typical implementation |
<Route path="*" element={<NotFound />} /> |
A route-level error boundary in a supported router configuration. |
| User message |
Page not found. |
Something went wrong. |
A production application may need both approaches to handle invalid URLs and unexpected errors effectively.
12. Common Mistakes and Solutions
| Mistake |
Reason |
Solution |
| Child route does not render. |
The parent layout does not include an Outlet. |
Import and render <Outlet /> inside the parent component. |
| Incorrect child route path. |
The child path is configured incorrectly. |
Use relative child paths such as users inside the /dashboard route. |
| Parent URL displays no content. |
No index route is configured. |
Add an index route for default child content. |
| Not Found page never appears. |
The wildcard route is missing or another route matches the URL. |
Review the route configuration and test an unmatched URL. |
| Not Found page removes the shared layout. |
The wildcard route is outside the intended parent layout. |
Place a wildcard child route inside the parent route when the layout should remain visible. |
| Direct page refresh fails. |
The hosting server is not configured to serve the client application for nested URLs. |
Configure the server or hosting platform for the application's routing strategy. |
| 404 interface returns HTTP 200. |
A client-side wildcard component does not automatically set the HTTP status. |
Use server-side or framework-level handling when a real HTTP 404 response is required. |
13. Best Practices
- Use nested routes to share layouts across related pages.
- Include an Outlet wherever child route content should render.
- Use index routes to define default content for parent paths.
- Keep route names consistent and easy to understand.
- Create a helpful Not Found page with a clear message and a link to a valid route.
- Use nested wildcard routes when a section needs its own Not Found interface.
- Use error boundaries for supported route errors that are not simply unmatched URLs.
- Test valid paths, invalid paths, and direct browser refreshes.
- Configure the hosting server appropriately for client-side routing.
- Use the import paths and routing conventions supported by your installed React Router version.
14. Mini Project: Admin Dashboard with a 404 Page
Objective: Build a React application with a shared dashboard layout, nested pages, and a Not Found page for invalid URLs.
Project Structure
src/
App.jsx
DashboardLayout.jsx
DashboardHome.jsx
Users.jsx
Settings.jsx
NotFound.jsx
App.jsx
import { BrowserRouter, Routes, Route } from "react-router";
import DashboardLayout from "./DashboardLayout";
import DashboardHome from "./DashboardHome";
import Users from "./Users";
import Settings from "./Settings";
import NotFound from "./NotFound";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/dashboard" element={<DashboardLayout />}>
<Route index element={<DashboardHome />} />
<Route path="users" element={<Users />} />
<Route path="settings" element={<Settings />} />
<Route path="*" element={<NotFound />} />
</Route>
<Route path="*" element={<NotFound />} />
</Routes>
</BrowserRouter>
);
}
export default App;
DashboardLayout.jsx
import { Link, Outlet } from "react-router";
function DashboardLayout() {
return (
<div>
<h1>Admin Dashboard</h1>
<nav>
<Link to="/dashboard">Home</Link> |
<Link to="/dashboard/users">Users</Link> |
<Link to="/dashboard/settings">Settings</Link>
</nav>
<hr />
<Outlet />
</div>
);
}
export default DashboardLayout;
NotFound.jsx
import { Link } from "react-router";
function NotFound() {
return (
<div>
<h2>404 - Page Not Found</h2>
<p>The page you requested could not be found.</p>
<Link to="/dashboard">Back to Dashboard</Link>
</div>
);
}
export default NotFound;
Create the DashboardHome.jsx, Users.jsx, and Settings.jsx components using the examples in Section 5.
Project Test Cases
| Test URL |
Expected Output |
/dashboard |
Dashboard home inside the shared layout. |
/dashboard/users |
User Management inside the shared layout. |
/dashboard/settings |
Settings inside the shared layout. |
/dashboard/unknown |
Not Found page inside the dashboard layout. |
/unknown |
Top-level Not Found page. |
Expected Learning Outcomes
- Create parent and child routes.
- Reuse a common layout through Outlet.
- Configure index routes for default pages.
- Handle invalid URLs with wildcard routes.
- Understand the difference between a client-side 404 interface and an HTTP 404 response.
15. Interview Questions and Answers
Q1. What are nested routes in React Router?
Answer: Nested routes are routes defined inside a parent route. They allow related pages to share a common layout and render different child content.
Q2. What is the purpose of Outlet?
Answer: Outlet renders the matched child route inside the parent component.
Q3. What is an index route?
Answer: An index route is the default child route that renders at the parent URL without adding another path segment.
Q4. What is a Not Found Page?
Answer: A Not Found Page is displayed when a requested URL does not match an available route. It is commonly known as a 404 page.
Q5. How do you create a wildcard route?
Answer: In declarative React Router, use <Route path="*" element={<NotFound />} />.
Q6. Can a Not Found route be nested?
Answer: Yes. A wildcard child route can handle unmatched paths within a particular section while preserving its parent layout.
Q7. What happens if a parent component does not render Outlet?
Answer: The matched child route will not appear at the intended location inside the parent layout.
Q8. What is the difference between a Not Found route and an error boundary?
Answer: A Not Found route handles unmatched URLs, while an error boundary handles supported rendering or route data errors.
Q9. Does a client-side wildcard route guarantee an HTTP 404 status?
Answer: No. It renders a Not Found interface, but server or framework-level handling may be needed to return an actual HTTP 404 status.
Q10. Where are nested routes useful?
Answer: They are useful in dashboards, e-commerce websites, learning platforms, social applications, banking applications, and other applications that have shared layouts.
16. Practice Exercises
- Create an
/admin parent route with child routes for Home, Users, and Settings.
- Use Outlet to display child content inside a shared sidebar layout.
- Add an index route for the admin home page.
- Create a wildcard route that displays a 404 page.
- Add a nested wildcard route for invalid admin pages.
- Add a link to return to the home page from the Not Found component.
- Test valid URLs, invalid URLs, and refreshing a nested route directly in the browser.
17. Quick Revision Checklist
- Understand parent routes and child routes.
- Know how to configure nested routes using Route components.
- Understand how Outlet renders child components.
- Know how to create an index route.
- Understand how wildcard routes work.
- Know how to create a Not Found component.
- Understand the difference between a 404 interface and an HTTP 404 status.
- Know when to use nested wildcard routes.
- Test routes and direct page refreshes.
18. Conclusion
Nested Routes and Not Found Pages are important features for building organized and user-friendly React applications. Nested routes help developers reuse layouts and manage related pages efficiently, while Not Found Pages provide clear feedback when users visit invalid URLs. Combining these techniques improves application structure, maintainability, and navigation.
Continue learning through JustAcademy React JS Training or Register for React JS Course Demo.
Official References