Capítulo 14 de 57
Migrating from React Location (TanStack Router's spiritual predecessor, sharing many design decisions) follows a concrete step-by-step recipe: swap dependencies, set up file-based route generation via the Vite plugin or CLI, and recreate the app's route files (__root.tsx, index.tsx, posts.tsx, posts.index.tsx, posts.$postId.tsx) one at a time before wiring the router instance into the entry file.
declare module '@tanstack/react-router' { interface Register { router: typeof router } }).tsr.config.json: Minimal config { "routesDirectory": "./src/routes", "generatedRouteTree": "./src/routeTree.gen.ts" } needed when not relying purely on bundler-plugin defaults.posts.tsx (layout with loader + Outlet), posts.index.tsx (the /posts/ index child), and posts.$postId.tsx (dynamic child with its own loader) demonstrate the flat-file naming convention for nested routes.Route.useLoaderData(): The pattern for accessing loader-fetched data inside a route's component, shown in the posts.tsx and posts.$postId.tsx examples.// src/routes/posts.$postId.tsx
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/posts/$postId')({
component: PostsId,
loader: async ({ params: { postId } }) => {
const post = await fetchPost(postId)
return { post }
},
})
function PostsId() {
const { post } = Route.useLoaderData()
// ...
}
loader reading params.postId and exposing its result via Route.useLoaderData().@tanstack/react-location(-devtools) with @tanstack/react-router(-devtools), add @tanstack/router-plugin, create a routes/ directory, and rebuild each route as its own file.posts.tsx, posts.index.tsx, posts.$postId.tsx) maps directly onto React Location's single-array route config, just split across files.