Capítulo 7 de 57
Manual setup requires installing @tanstack/react-router (plus optionally @tanstack/react-router-devtools and, for file-based routing, @tanstack/router-plugin), then wiring a root route, at least one child route, and a main.tsx entry that creates the router, registers it via module declaration, and renders RouterProvider, either through file-based routing or a single-file code-based configuration.
src/routes/__root.tsx (root layout, note the double underscore), src/routes/index.tsx, src/routes/about.tsx, and src/main.tsx are the minimum files needed.tanstackRouter({ target: 'react', autoCodeSplitting: true }) from @tanstack/router-plugin/vite must be passed to Vite's plugins array before @vitejs/plugin-react.src/routeTree.gen.ts and is imported into main.tsx as routeTree.createRootRoute: Defines the root layout component (rendering <Outlet /> for children); used identically in both file-based and code-based setups.createRouter + module declaration: const router = createRouter({ routeTree }) followed by declare module '@tanstack/react-router' { interface Register { router: typeof router } } in main.tsx is the standard boilerplate for type-safe registration.createRoute({ getParentRoute: () => rootRoute, path: '/', component: ... }) plus rootRoute.addChildren([indexRoute, aboutRoute]) builds the route tree manually; docs recommend splitting routes into separate files as the app grows even though a single file works for demos.<div> id: When using file-based routing generated output, the root HTML element should be <div id="root"></div> (code-based example in the docs instead targets id="app").// src/main.tsx
import { StrictMode } from 'react'
import ReactDOM from 'react-dom/client'
import { RouterProvider, createRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'
const router = createRouter({ routeTree })
declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
}
const rootElement = document.getElementById('root')!
if (!rootElement.innerHTML) {
const root = ReactDOM.createRoot(rootElement)
root.render(
<StrictMode>
<RouterProvider router={router} />
</StrictMode>,
)
}
RouterProvider.routeTree, call createRouter({ routeTree }), then declare the Register module interface before rendering RouterProvider.tanstackRouter plugin must precede @vitejs/plugin-react in the plugins array, ordering matters.TanStackRouterDevtools slots into the same __root.tsx file built here.getParentRoute and module declaration are structured this way.