Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/tanstack/router/llms.txt

Use this file to discover all available pages before exploring further.

Route API

Routes define the URL structure, data loading, and rendering for your application.

createRoute

Creates a non-root Route instance for code-based routing.
import { createRoute } from '@tanstack/react-router'
import { rootRoute } from './root'

const postsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/posts',
  component: PostsComponent,
})

Parameters

options
RouteOptions
required
Route configuration options.

Returns

route
Route
A Route instance to be attached to the route tree.

Route Methods

Route instances expose several type-safe methods for use in components:

route.useMatch()

Get the current route match data.
function Component() {
  const match = postsRoute.useMatch()
  return <div>{match.status}</div>
}

route.useParams()

Access the route’s path parameters.
function Component() {
  const { postId } = postRoute.useParams()
  return <div>Post ID: {postId}</div>
}

route.useSearch()

Access the route’s search parameters.
function Component() {
  const { page, filter } = postsRoute.useSearch()
  return <div>Page {page}</div>
}

route.useLoaderData()

Access the route’s loader data.
function Component() {
  const { post } = postRoute.useLoaderData()
  return <h1>{post.title}</h1>
}

route.useLoaderDeps()

Access the route’s loader dependencies.
function Component() {
  const deps = postsRoute.useLoaderDeps()
  return <div>Page: {deps.page}</div>
}

route.useNavigate()

Get a navigate function pre-bound to this route.
function Component() {
  const navigate = postRoute.useNavigate()
  return <button onClick={() => navigate({ to: '..' })}>Back</button>
}

route.useRouteContext()

Access the route’s context.
function Component() {
  const context = postRoute.useRouteContext()
  return <div>{context.postId}</div>
}
A pre-bound Link component for this route.
function Component() {
  return (
    <postRoute.Link params={{ postId: '123' }}>
      View Post
    </postRoute.Link>
  )
}

createRouteMask

Create a route mask for displaying a route at a different path.
import { createRouteMask } from '@tanstack/react-router'

const photoModalMask = createRouteMask({
  routeTree,
  from: '/photos/$photoId',
  to: '/photos',
  params: true,
})

Parameters

options
object
required

Returns

routeMask
RouteMask
A route mask object to pass to the router’s routeMasks option.

Component Types

RouteComponent

A standard route component type.
import type { RouteComponent } from '@tanstack/react-router'

const MyComponent: RouteComponent = () => {
  return <div>Hello</div>
}

ErrorRouteComponent

An error boundary component type with error props.
import type { ErrorRouteComponent } from '@tanstack/react-router'

const ErrorComponent: ErrorRouteComponent = ({ error }) => {
  return <div>Error: {error.message}</div>
}

NotFoundRouteComponent

A not-found component type with routing props.
import type { NotFoundRouteComponent } from '@tanstack/react-router'

const NotFound: NotFoundRouteComponent = () => {
  return <div>Page not found</div>
}