Read route filters
A route component needs validated query values such as filters, pagination, or sorting.
TanStack · Hook
useSearch returns the current location's search parameters in a TanStack Router React component. Use from for route-specific typing, select for a derived value, and strict: false for a looser shared-schema view.
useSearch is a TanStack Router React hook that subscribes to the current location's search parameters. Provide a route identifier with from for precise types, optionally project the search object with select, or set strict to false for a looser shared-schema result.
useSearch({ from, shouldThrow, select, strict })| Name | Type | Description |
|---|---|---|
from | string | Route ID used to match the search-query parameters; required in strict mode. |
shouldThrow | boolean | Whether to throw when no matching rendered route is found; defaults to true. |
select | (search: SelectedSearchSchema) => TSelected | Transforms the search object and returns the selected value. |
strict | boolean | When false, ignores from and loosens the type to Partial<FullSearchSchema>; defaults to true. |
With from, the current location's search-parameter object, or the value returned by select. With strict: false, a Partial<FullSearchSchema> or the selected value; with shouldThrow: false and no match, undefined.
import { useSearch } from '@tanstack/react-router'
function Component() {
const search = useSearch({ from: '/posts/$postId' })
return <pre>{JSON.stringify(search, null, 2)}</pre>
}Reads the current route's typed search parameters and renders them.
A route component needs validated query values such as filters, pagination, or sorting.
A component needs only one derived search value instead of the full parameter object.
Shared UI reads search parameters without naming a specific route origin.
useSearch returns the current location's search query parameters as an object. Passing from identifies the route whose search schema should be used, providing stronger type safety.
Follow-up: How can a component subscribe to only one search value? Use select to return that value, so the hook result is the selected type and the component can avoid responding to unrelated search changes.
The select callback receives the search object and its return value becomes the hook result. For example, selecting search.postView produces a string rather than the complete search schema.
Follow-up: What trade-off does strict: false make? It omits the from requirement and exposes a Partial<FullSearchSchema>, so properties may be undefined.
The hook accepts an options object. from is required in strict mode, shouldThrow defaults to true, select is optional, and strict defaults to true.
Follow-up: What happens when a route match cannot be found? With the default shouldThrow behavior an invariant exception is thrown; setting it to false returns undefined instead.
useRouterStateRoute.useSearchuseParamsuseLoaderData