Capítulo 44 de 57
The createRouter function instantiates the core Router object, which needs a routeTree, and its type must be registered via TypeScript declaration merging (Register interface) to enable full type safety across the application.
createRouter: The function that creates a Router instance, the "brains" of TanStack Router responsible for route matching, navigation, and router-wide configuration.routeTree: Required option passed to createRouter, produced either by file-based routing (imported from the generated src/routeTree.gen.ts) or code-based routing (built manually via rootRoute.addChildren([...])).Register interface from @tanstack/react-router with a router: typeof router property so the entire project gets type-safe hooks, components, and imports.notFoundComponent: Passed to createRootRoute to render a fallback (e.g. a 404 page) when no route matches.createRouter beyond routeTree, documented in the RouterOptions API reference.import { createRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'
const router = createRouter({
routeTree,
})
declare module '@tanstack/react-router' {
interface Register {
// This infers the type of our router and registers it across your entire project
router: typeof router
}
}
export const Route = createRootRoute({
component: () => (
// ...
),
notFoundComponent: () => <div>404 Not Found</div>,
})
addChildren).notFoundComponent on the root route handles unmatched paths; skipping this step leaves navigation without a fallback UI.routeTree.gen.ts is generated and configured.notFoundComponent.