Course contentsShow
← React
Lesson 1832 of 2,39643. RoutingFree lesson

The Route Tree: Parent and Child Routes

How React Router builds a tree of routes where children inherit their parent's path prefix automatically.

The Route Tree: Parent and Child Routes

You'll learn how React Router automatically builds a hierarchical tree of routes where child routes inherit their parent's path prefix.

How the Route Tree Works

When you nest routes inside other routes, React Router creates a tree structure where each child automatically inherits the path of its parent. This means you don't repeat path segments—the router composes them for you.

Think of it like a file system: if you have a folder /dashboard, and inside it a file settings, the full path becomes /dashboard/settings. You don't write the full path twice—the folder structure handles it. Route trees work the same way.

Parent Routes Set the Base Path

A parent route establishes a path prefix. Any routes nested inside it automatically get that prefix added to their own path:

<Route path="/dashboard" element={<DashboardLayout />}>
  <Route path="settings" element={<Settings />} />
  <Route path="profile" element={<Profile />} />
</Route>

Here, settings becomes /dashboard/settings and profile becomes /dashboard/profile. The parent's /dashboard is automatically prepended.

Why This Matters

The route tree structure creates a clear hierarchy that mirrors your UI structure. Parent routes often render layout components that wrap their children (like a sidebar or navigation), while child routes render the specific content inside that layout.

This inheritance prevents path duplication and keeps your routing configuration DRY (Don't Repeat Yourself). You define path segments once at each level, and the tree composition handles the rest.

Key Takeaway: React Router builds a tree where child routes inherit their parent's path prefix automatically, creating hierarchical URLs that mirror your component structure without repeating path segments.