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.
- Type: string
- Required
- The RouteID to match the search query parameters from.
- Type: boolean
- Optional
- default: true
- If false,useSearch will not throw an invariant exception in case a match was not found in the currently rendered matches; in this case, it will return undefined.
- Type: (search: SelectedSearchSchema) => TSelected
- Optional
- If supplied, this function will be called with the search object and the return value will be returned from useSearch.
- Type: boolean
- Optional
- Configures whether structural sharing is enabled for the value returned by select.
- See the Render Optimizations guide for more information.
- Type: boolean
- Optional - default: true
- If false, the opts.from option will be ignored and types will be loosened to Partial<FullSearchSchema> to reflect the shared types of all search query parameters.
- If opts.from is provided, an object of the search query parameters for the current location or TSelected if a select function is provided.
- If opts.strict is false, an object of the search query parameters for the current location or TSelected if a select function is provided.
import { useSearch } from '@tanstack/react-router'
function Component() {
const search = useSearch({ from: '/posts/$postId' })
const selected = useSearch({
from: '/posts/$postId',
select: (search) => search.postView,
})
const looseSearch = useSearch({ strict: false })
}