The useSearch method is a hook that returns the current search query parameters as an object for the current location. This hook is useful for accessing the current search string and query parameters in a component.
The useSearch hook accepts an options object.
import { useSearch } from '@tanstack/react-router'
function Component() {
const search = useSearch({ from: '/posts/$postId' })
// ^ FullSearchSchema
// OR
const selected = useSearch({
from: '/posts/$postId',
select: (search) => search.postView,
})
// ^ string
// OR
const looseSearch = useSearch({ strict: false })
// ^ Partial<FullSearchSchema>
// ...
}
import { useSearch } from '@tanstack/react-router'
function Component() {
const search = useSearch({ from: '/posts/$postId' })
// ^ FullSearchSchema
// OR
const selected = useSearch({
from: '/posts/$postId',
select: (search) => search.postView,
})
// ^ string
// OR
const looseSearch = useSearch({ strict: false })
// ^ Partial<FullSearchSchema>
// ...
}
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.