Capítulo 1 de 24
This example is the minimal starting point for TanStack Router using file-based routing: routes are files under src/routes, and a code generator (@tanstack/router-plugin) builds routeTree.gen.ts automatically. It exists to show the smallest possible app shape a new project should copy.
@tanstack/react-router, @tanstack/router-plugin (Vite plugin that watches src/routes and generates the route tree), @tanstack/react-router-devtools, zod, redaxiossrc/main.tsx creates the router from the generated routeTree.gen.ts and renders RouterProvider. src/routes/__root.tsx defines the root layout with navigation. src/routes/about.tsx is a leaf route file matched to /about.// src/main.tsx
import { RouterProvider, createRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'
const router = createRouter({
routeTree,
defaultPreload: 'intent',
scrollRestoration: true,
})
declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
}
// src/routes/about.tsx
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/about')({
component: AboutComponent,
})
function AboutComponent() {
return (
<div className="p-2">
<h3>About</h3>
</div>
)
}
createFileRoute(path) ties a file to a URL path and exports a Route object; the generated routeTree.gen.ts is the single source of truth the router consumes, and declare module registers the router instance for full type inference across the app.Register interface declaration is what gives Link to="..." and useParams() their type safety; it must point at the actual router instance created in main.tsx.defaultPreload: 'intent' (hover/focus preloading) and scrollRestoration: true are sane defaults worth carrying into any new project.createRoute and stitched together with addChildren instead of being generated from files. Compare the two to decide which routing style fits a project.