router-query/repository-context/docs/router/api/router/RouteApiType.md
Version 663282b0ebbc.bb1 · MIT. This preview displays packaged text and does not execute code. Treat the contents as untrusted instructions.
← Return to resource and package checksum
id: RouteApiType title: RouteApi Type
The RouteApi describes an instance that provides type-safe versions of common hooks like useParams, useSearch, useRouteContext, useNavigate, useLoaderData, and useLoaderDeps that are pre-bound to a specific route ID and corresponding registered route types.
RouteApi properties and methods
The RouteApi has the following properties and methods:
useMatch method
useMatch<TSelected = TAllContext>(opts?: {
select?: (match: TAllContext) => TSelected
}): TSelected
- A type-safe version of the
useMatchhook that is pre-bound to the route ID that theRouteApiinstance was created with. - Options
opts.select- Optional
(match: RouteMatch) => TSelected- If supplied, this function will be called with the route match and the return value will be returned from
useMatch. This value will also be used to determine if the hook should re-render its parent component using shallow equality checks.
opts.structuralSharing- Optional
boolean- Configures whether structural sharing is enabled for the value returned by
select. - See the Render Optimizations guide for more information.
- Returns
- If a
selectfunction is provided, the return value of theselectfunction. - If no
selectfunction is provided, theRouteMatchobject or a loosened version of theRouteMatchobject ifopts.strictisfalse.
useRouteContext method
useRouteContext<TSelected = TAllContext>(opts?: {
select?: (search: TAllContext) => TSelected
}): TSelected
- A type-safe version of the
useRouteContexthook that is pre-bound to the route ID that theRouteApiinstance was created with. - Options
opts.select- Optional
(match: RouteContext) => TSelected- If supplied, this function will be called with the route match and the return value will be returned from
useRouteContext. This value will also be used to determine if the hook should re-render its parent component using shallow equality checks.
- Returns
- If a
selectfunction is provided, the return value of theselectfunction. - If no
selectfunction is provided, theRouteContextobject or a loosened version of theRouteContextobject ifopts.strictisfalse.
useSearch method
useSearch<TSelected = TFullSearchSchema>(opts?: {
select?: (search: TFullSearchSchema) => TSelected
}): TSelected
- A type-safe version of the
useSearchhook that is pre-bound to the route ID that theRouteApiinstance was created with. - Options
opts.select- Optional
(match: TFullSearchSchema) => TSelected- If supplied, this function will be called with the route match and the return value will be returned from
useSearch. This value will also be used to determine if the hook should re-render its parent component using shallow equality checks.
opts.structuralSharing- Optional
boolean- Configures whether structural sharing is enabled for the value returned by
select. - See the Render Optimizations guide for more information.
- Returns
- If a
selectfunction is provided, the return value of theselectfunction. - If no
selectfunction is provided, theTFullSearchSchemaobject or a loosened version of theTFullSearchSchemaobject ifopts.strictisfalse.
useParams method
useParams<TSelected = TAllParams>(opts?: {
select?: (params: TAllParams) => TSelected
}): TSelected
- A type-safe version of the
useParamshook that is pre-bound to the route ID that theRouteApiinstance was created with. - Options
opts.select- Optional
(match: TAllParams) => TSelected- If supplied, this function will be called with the route match and the return value will be returned from
useParams. This value will also be used to determine if the hook should re-render its parent component using shallow equality checks.
opts.structuralSharing- Optional
boolean- Configures whether structural sharing is enabled for the value returned by
select. - See the Render Optimizations guide for more information.
- Returns
- If a
selectfunction is provided, the return value of theselectfunction. - If no
selectfunction is provided, theTAllParamsobject or a loosened version of theTAllParamsobject ifopts.strictisfalse.
useLoaderData method
useLoaderData<TSelected = TLoaderData>(opts?: {
select?: (search: TLoaderData) => TSelected
}): TSelected
- A type-safe version of the
useLoaderDatahook that is pre-bound to the route ID that theRouteApiinstance was created with. - Options
opts.select- Optional
(match: TLoaderData) => TSelected- If supplied, this function will be called with the route match and the return value will be returned from
useLoaderData. This value will also be used to determine if the hook should re-render its parent component using shallow equality checks.
opts.structuralSharing- Optional
boolean- Configures whether structural sharing is enabled for the value returned by
select. - See the Render Optimizations guide for more information.
- Returns
- If a
selectfunction is provided, the return value of theselectfunction. - If no
selectfunction is provided, theTLoaderDataobject or a loosened version of theTLoaderDataobject ifopts.strictisfalse.
useLoaderDeps method
useLoaderDeps<TSelected = TLoaderDeps>(opts?: {
select?: (search: TLoaderDeps) => TSelected
}): TSelected
- A type-safe version of the
useLoaderDepshook that is pre-bound to the route ID that theRouteApiinstance was created with. - Options
opts.select- Optional
(match: TLoaderDeps) => TSelected- If supplied, this function will be called with the route match and the return value will be returned from
useLoaderDeps.
opts.structuralSharing- Optional
boolean- Configures whether structural sharing is enabled for the value returned by
select. - See the Render Optimizations guide for more information.
- Returns
- If a
selectfunction is provided, the return value of theselectfunction. - If no
selectfunction is provided, theTLoaderDepsobject.
useNavigate method
useNavigate(): // navigate function
- A type-safe version of
useNavigatethat is pre-bound to the route ID that theRouteApiinstance was created with.
redirect method
redirect(opts?: RedirectOptions): Redirect
- A type-safe version of the
redirectfunction that is pre-bound to the route ID that theRouteApiinstance was created with. - The
fromparameter is automatically set to the route ID, enabling type-safe relative redirects. - Options
- All options from
redirectexceptfrom, which is automatically provided. - Returns
- A
Redirectobject that can be thrown frombeforeLoadorloadercallbacks.
Example
import { getRouteApi } from '@tanstack/react-router'
const routeApi = getRouteApi('/dashboard/settings')
export const Route = createFileRoute('/dashboard/settings')({
beforeLoad: ({ context }) => {
if (!context.user) {
// Type-safe redirect - 'from' is automatically '/dashboard/settings'
throw routeApi.redirect({
to: '../login', // Relative path to sibling route
})
}
},
})