The useMatchRoute hook is a hook that returns a matchRoute function that can be used to match a route against either the current or pending location.
The matchRoute function is a function that can be used to match a route against either the current or pending location.
The matchRoute function accepts a single argument, an options object.
import { useMatchRoute } from '@tanstack/react-router'
// Current location: /posts/123
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({ to: '/posts/$postId' })
// ^ { postId: '123' }
}
// Current location: /posts/123
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({ to: '/posts' })
// ^ false
}
// Current location: /posts/123
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({ to: '/posts', fuzzy: true })
// ^ {}
}
// Current location: /posts
// Pending location: /posts/123
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({ to: '/posts/$postId', pending: true })
// ^ { postId: '123' }
}
// Current location: /posts/123/foo/456
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({ to: '/posts/$postId/foo/$fooId' })
// ^ { postId: '123', fooId: '456' }
}
// Current location: /posts/123/foo/456
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({
to: '/posts/$postId/foo/$fooId',
params: { postId: '123' },
})
// ^ { postId: '123', fooId: '456' }
}
// Current location: /posts/123/foo/456
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({
to: '/posts/$postId/foo/$fooId',
params: { postId: '789' },
})
// ^ false
}
// Current location: /posts/123/foo/456
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({
to: '/posts/$postId/foo/$fooId',
params: { fooId: '456' },
})
// ^ { postId: '123', fooId: '456' }
}
// Current location: /posts/123/foo/456
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({
to: '/posts/$postId/foo/$fooId',
params: { postId: '123', fooId: '456' },
})
// ^ { postId: '123', fooId: '456' }
}
// Current location: /posts/123/foo/456
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({
to: '/posts/$postId/foo/$fooId',
params: { postId: '789', fooId: '456' },
})
// ^ false
}
import { useMatchRoute } from '@tanstack/react-router'
// Current location: /posts/123
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({ to: '/posts/$postId' })
// ^ { postId: '123' }
}
// Current location: /posts/123
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({ to: '/posts' })
// ^ false
}
// Current location: /posts/123
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({ to: '/posts', fuzzy: true })
// ^ {}
}
// Current location: /posts
// Pending location: /posts/123
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({ to: '/posts/$postId', pending: true })
// ^ { postId: '123' }
}
// Current location: /posts/123/foo/456
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({ to: '/posts/$postId/foo/$fooId' })
// ^ { postId: '123', fooId: '456' }
}
// Current location: /posts/123/foo/456
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({
to: '/posts/$postId/foo/$fooId',
params: { postId: '123' },
})
// ^ { postId: '123', fooId: '456' }
}
// Current location: /posts/123/foo/456
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({
to: '/posts/$postId/foo/$fooId',
params: { postId: '789' },
})
// ^ false
}
// Current location: /posts/123/foo/456
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({
to: '/posts/$postId/foo/$fooId',
params: { fooId: '456' },
})
// ^ { postId: '123', fooId: '456' }
}
// Current location: /posts/123/foo/456
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({
to: '/posts/$postId/foo/$fooId',
params: { postId: '123', fooId: '456' },
})
// ^ { postId: '123', fooId: '456' }
}
// Current location: /posts/123/foo/456
function Component() {
const matchRoute = useMatchRoute()
const params = matchRoute({
to: '/posts/$postId/foo/$fooId',
params: { postId: '789', fooId: '456' },
})
// ^ false
}
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.