Index Routes: Default Child Content
What you'll learn: How to render a default component when a parent route matches but no child route is selected.
The Problem: Empty Parent Routes
When you set up nested routes with <Outlet />, what happens when users visit just the parent path? For example, if you have /dashboard with child routes like /dashboard/stats and /dashboard/settings, what renders at /dashboard itself? Without an index route, the <Outlet /> is simply empty—a blank space where child content should be.
What Is an Index Route?
An index route is a special route that renders by default when only the parent path matches. It has no additional path segment—it "fills in" the <Outlet /> when no specific child is selected.
Think of it like a homepage for a section of your app. When you visit a book's table of contents page, you see an introduction before diving into specific chapters. The index route is that introduction.
Defining an Index Route
In React Router, you create an index route using the index prop instead of a path prop:
<Route path="/dashboard" element={<DashboardLayout />}>
<Route index element={<DashboardHome />} />
<Route path="stats" element={<Stats />} />
<Route path="settings" element={<Settings />} />
</Route>
Now when users visit /dashboard, they see <DashboardHome /> rendered in the parent's <Outlet />. Visit /dashboard/stats, and <Stats /> replaces it.
Why Use Index Routes?
- No awkward redirects: Instead of redirecting
/dashboardto/dashboard/home, serve content directly - Cleaner URLs:
/dashboardis simpler than/dashboard/home - Natural defaults: Every parent with children can have a sensible starting point
Key Takeaway: Index routes render default content at a parent route path, filling the <Outlet /> when no child route is selected, providing a natural "landing page" for nested route sections.