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

Defining Routes with createRoute

Create route definitions with paths, loaders, and components using the typed route builder API.

Defining Routes with createRoute

You'll learn how to define individual routes in TanStack Router using the typed route builder API, unlocking full end-to-end type safety for paths, loaders, and components.

The Core Idea

In TanStack Router, each route is a first-class object created with createRoute(). Unlike React Router's JSX-based configuration, you build routes as typed entities that explicitly declare their path, what data they load, and what component they render. Think of createRoute() as a blueprint factory — you hand it specifications (path, loader, component) and it returns a fully typed route definition that knows exactly what data flows through it.

This approach means TypeScript can verify that your component receives the correct loader data, that your paths match what you define, and that everything connects properly before runtime.

How It Works

You call createRoute() with a configuration object containing three key properties:

  • getParentRoute() — Links to a parent route (often a root route)
  • path — The URL segment for this route (like "/about" or "/users/$userId")
  • component — The React component to render when this route matches

You can also add a loader function that fetches data before rendering. The loader receives typed context (like route parameters) and returns data that your component can access with full type safety.

The result is a route object you can later compose into a route tree.

Why This Matters

By defining routes as objects rather than JSX elements, TanStack Router makes every piece of routing — from URL params to loader responses — fully typed. Your editor will autocomplete route paths, catch mismatched data types, and prevent runtime routing errors before they happen.

Key Takeaway: createRoute() transforms route definitions into typed objects with paths, loaders, and components, giving you compile-time guarantees for your entire routing layer.