Capítulo 2 de 24
The code-based counterpart to the file-based quickstart: the exact same two-page app (Home/About), but every route is declared as a JavaScript object with createRoute and assembled into a tree by hand with addChildren. It shows the alternative to file-based routing when a team prefers routes defined entirely in code.
src/routes folder, no @tanstack/router-plugin, no code generation)@tanstack/react-router, @tanstack/react-router-devtools, React 19, Tailwind v4. Notably absent compared to the file-based version: @tanstack/router-plugin, zod, redaxios.src/main.tsx. A rootRoute is created with createRootRoute, child routes (indexRoute, aboutRoute) are created with createRoute and given getParentRoute, then combined into routeTree with rootRoute.addChildren([...]).// src/main.tsx
const rootRoute = createRootRoute({
component: () => (
<>
<div className="p-2 flex gap-2">
<Link to="/" className="[&.active]:font-bold">Home</Link>{' '}
<Link to="/about" className="[&.active]:font-bold">About</Link>
</div>
<hr />
<Outlet />
<TanStackRouterDevtools />
</>
),
})
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: function Index() {
return <div className="p-2"><h3>Welcome Home!</h3></div>
},
})
const aboutRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/about',
component: function About() {
return <div className="p-2">Hello from About!</div>
},
})
const routeTree = rootRoute.addChildren([indexRoute, aboutRoute])
const router = createRouter({ routeTree, defaultPreload: 'intent', scrollRestoration: true })
routeTree manually with createRootRoute + createRoute + addChildren, no filesystem convention involved. Link still gets full path type safety because routeTree is a typed value passed straight into createRouter.getParentRoute: () => parentRoute to attach into the tree; nesting is expressed by nesting these calls, unlike file-based routing where nesting comes from filenames.createRoute and createFileRoute share nearly identical options (component, loader, validateSearch, etc.); switching between the two styles is mostly a matter of where the object is created, not what it can do.src/routes/*.tsx files and a generated routeTree.gen.ts instead of hand-written addChildren calls..lazy()) and nested pathless layouts built the same manual way.