Build breadcrumbs
A layout or root component needs route metadata for breadcrumbs or navigation.
TanStack · Hook
useMatches returns the router’s complete presented route-match list from any component-tree position. An optional selector can transform that list into a derived result.
useMatches exposes the router’s complete presented route-match array from any component-tree position. Pass an optional selector to return a derived value instead; the selected result participates in shallow-equality checks, and structural sharing can be configured for it.
useMatches(opts?)| Name | Type | Description |
|---|---|---|
opts | object | Optional options object. It may contain select and structuralSharing. |
opts.select | (matches: RouteMatch[]) => TSelected | Optional function receiving the route matches and returning the selected value. |
opts.structuralSharing | boolean | Optional flag controlling structural sharing for the selected value. |
With select, the selected value; without select, an array of RouteMatch objects.
import { useMatches } from '@tanstack/react-router'
export function Breadcrumbs() {
const matches = useMatches({
select: (matches) =>
matches.filter((match) => match.staticData?.getTitle),
})
return (
<nav>
{matches.map((match) => (
<span key={match.id}>{match.staticData.getTitle()}</span>
))}
</nav>
)
}Selects only matches that provide a static title, then renders those titles as breadcrumbs.
A layout or root component needs route metadata for breadcrumbs or navigation.
A shared component must derive a boolean or subset from all active matches.
A component needs to inspect loading, error, or not-found match presentation.
useMatches returns the router’s complete presented array of RouteMatch objects, regardless of where the calling component sits in the component tree. Without a selector, the result is an array of RouteMatch objects.
Follow-up: How would you obtain only ancestors? Use useParentMatches, which is intended for parent-match selection rather than the complete match list.
The hook accepts one optional options object. Its select function receives the route matches and returns the value exposed by the hook, which can be a derived value such as filtered metadata.
Follow-up: Why use select instead of filtering after the hook returns? Selection lets the hook use shallow equality checks on the selected result to decide whether the parent should re-render.
structuralSharing is an optional boolean in the options object. It controls whether structural sharing is enabled for the value returned by select.
Follow-up: What should a component do if it needs only child matches? Use useChildMatches rather than retrieving the full array and filtering it manually.
useParentMatchesuseChildMatchesRouteMatchuseRouterState