0
Fork 0
mirror of https://github.com/TanStack/router.git synced 2026-11-16 19:38:07 +00:00
router/docs/framework/react/api/router/useRouteContextHook.md
Sean Cassiere 58a15deb49
docs: add more missing reference pages (#1352)
* 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
2024-03-22 09:01:20 -06:00

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 TSelected if a select function 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

  // ...
}