Capítulo 15 de 57
TanStack Router builds a nested route tree from route segments, each with well-defined semantics (index, dynamic, splat, optional, layout, pathless, non-nested, excluded, grouped), that together let you express nearly any URL structure declaratively.
createFileRoute(path): Defines a route in file-based routing; the path string argument is auto-managed by the Router Bundler Plugin/CLI and is what gives TypeScript the context needed for type safety.createRootRoute() (or createRootRouteWithContext<T>() for typed router context). Has no path, is always matched, and its component always renders as the outermost wrapper./about).createFileRoute('/posts/').$param): A path segment prefixed with $ (e.g. $postId) captures that URL part into params, accessible via Route.useParams() or in a loader.$): A route whose path is just $ captures the rest of the URL into params._splat (or params['*'] in v1 for backward compatibility).{-$param}): Segment that may or may not be present in the URL; matches with the param as undefined when absent. Multiple optional params can be chained. Ranked lower priority than exact matches.app.tsx) that wraps children with a shared component via <Outlet />, and can enforce loaders, search param validation, and shared context on all children._prefix): Wraps children without adding a URL segment; the part after _ becomes the route's required unique ID. Cannot contain dynamic segments directly (the dynamic segment must be a separate path segment).parent_.child): A trailing underscore on the parent segment un-nests a route from its parent's component tree, e.g. posts_.$postId.edit.tsx renders standalone, not nested inside <Posts>.-prefix): Files/folders prefixed with - are ignored by route generation, letting you colocate non-route logic (e.g. -components/) and import from it in route files.(group)): Parenthesized directories are purely organizational, do not affect the route tree/URL/component tree.import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/posts/{-$category}')({
component: PostsComponent,
})
function PostsComponent() {
const { category } = Route.useParams()
return <div>{category ? `Posts in ${category}` : 'All Posts'}</div>
}
/posts and /posts/tech.$param) work independently at every path segment; splat routes ($ alone) grab everything remaining into _splat._prefix) to share layout/loader/context logic without adding a URL segment, and non-nested routes (parent_.child) to break out of a parent's component tree when needed.-, grouping directories with ()) let you colocate code and organize routes without affecting the generated route tree.