mirror of
https://github.com/TanStack/router.git
synced 2026-11-16 19:38:07 +00:00
* docs: add useNavigate to the RouteApiClass * docs: add the useLoaderDeps hook * docs: update RouteApi description * docs: update getRouteApi description * docs: add the useRouterState hook * docs: add the useRouter hook * docs: missing `hook` from title * docs: add the useRouteContext hook * docs: add the useSearch hook
1.1 KiB
1.1 KiB
| id | title |
|---|---|
| useRouteContextHook | useRouteContext hook |
The useRouteContext method is a hook that returns the current context for the current route. This hook is useful for accessing the current route context in a component.
useRouteContext options
The useRouteContext hook accepts an options object.
opts.from option
- Type:
string - Required
- The RouteID to match the route context from.
opts.select option
- Type:
(context: RouteContext) => TSelected - Optional
- If supplied, this function will be called with the route context object and the return value will be returned from
useRouteContext.
useRouteContext returns
- The current context for the current route or
TSelectedif aselectfunction is provided.
Examples
import { useRouteContext } from '@tanstack/react-router'
function Component() {
const context = useRouteContext({ from: '/posts/$postId' })
// ^ RouteContext
// OR
const selected = useRouteContext({
from: '/posts/$postId',
select: (context) => context.postId,
})
// ^ string
// ...
}