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

Route Trees and createRouter

Compose route definitions into a nested tree and create a typed router instance with full inference.

Route Trees and createRouter

You'll learn how to compose your individual route definitions into a nested tree structure and create a fully typed router instance that powers your entire application.

What Is a Route Tree?

In TanStack Router, you don't just define routes in isolation—you compose them into a tree that mirrors your URL hierarchy. Think of it like a family tree: a root route at the top, with child routes branching off beneath it. This tree structure is what the router uses to match URLs and understand parent-child relationships.

Building the Tree

After defining routes with createRoute, you construct the tree by passing child routes to their parents using the addChildren() method:

const rootRoute = createRoute({
  id: 'root',
  component: RootLayout,
});

const indexRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/',
  component: Home,
});

const aboutRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/about',
  component: About,
});

// Compose into tree
const routeTree = rootRoute.addChildren([indexRoute, aboutRoute]);

Creating the Router

Once you have your tree, you create the router instance with createRouter(). This is where TanStack Router's magic happens—it analyzes your entire route tree and generates end-to-end type inference for paths, params, search params, and more:

const router = createRouter({ routeTree });

Now router knows every possible route, parameter shape, and navigation path at compile time. You'll use this instance with <RouterProvider router={router} /> to power your app.

Key Takeaway: The route tree organizes your routes hierarchically, and createRouter transforms that tree into a type-safe router instance that TypeScript fully understands—no manual type annotations needed.