The useMatch hook returns a RouteMatch in the component tree. The raw route match contains all of the information about a route match in the router and also powers many other hooks under the hood like useParams, useLoaderData, useRouteContext, and useSearch.
The useMatch hook accepts a single argument, an options object.
import { useMatch } from '@tanstack/react-router'
function Component() {
const match = useMatch({ from: '/posts/$postId' })
// ^? strict match for RouteMatch
// ...
}
import { useMatch } from '@tanstack/react-router'
function Component() {
const match = useMatch({ from: '/posts/$postId' })
// ^? strict match for RouteMatch
// ...
}
import {
useMatch,
rootRouteId, // <<<< use this token!
} from '@tanstack/react-router'
function Component() {
const match = useMatch({ from: rootRouteId })
// ^? strict match for RouteMatch
// ...
}
import {
useMatch,
rootRouteId, // <<<< use this token!
} from '@tanstack/react-router'
function Component() {
const match = useMatch({ from: rootRouteId })
// ^? strict match for RouteMatch
// ...
}
import { useMatch } from '@tanstack/react-router'
function Component() {
const match = useMatch({ from: '/posts', shouldThrow: false })
// ^? RouteMatch | undefined
if (match !== undefined) {
// ...
}
}
import { useMatch } from '@tanstack/react-router'
function Component() {
const match = useMatch({ from: '/posts', shouldThrow: false })
// ^? RouteMatch | undefined
if (match !== undefined) {
// ...
}
}
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.