Framework
Version

useMatches hook

The useMatches hook returns all of the RouteMatch objects from the router regardless of its callers position in the React component tree.

Tip

If you only want the parent or child matches, then you can use the useParentMatches or the useChildMatches based on the selection you need.

useMatches options

The useMatches hook accepts a single optional argument, an options object.

opts.select option

  • Optional
  • (matches: RouteMatch[]) => TSelected
  • If supplied, this function will be called with the route matches and the return value will be returned from useMatches. This value will also be used to determine if the hook should re-render its parent component using shallow equality checks.

opts.structuralSharing option

  • Type: boolean
  • Optional
  • Configures whether structural sharing is enabled for the value returned by select.
  • See the Render Optimizations guide for more information.

useMatches returns

  • If a select function is provided, the return value of the select function.
  • If no select function is provided, an array of RouteMatch objects.

Examples

tsx
import { useMatches } from '@tanstack/react-router'

function Component() {
  const matches = useMatches()
  //     ^? [RouteMatch, RouteMatch, ...]
  // ...
}
import { useMatches } from '@tanstack/react-router'

function Component() {
  const matches = useMatches()
  //     ^? [RouteMatch, RouteMatch, ...]
  // ...
}
Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.