Skip to main content

Search Parameters

Search parameters (query strings) are a first-class feature in TanStack Router. They’re fully type-safe, validated, and integrated into the routing system.

Why Search Parameters Matter

Search parameters enable powerful UX patterns:
  • Shareable URLs - Users can bookmark and share filtered/sorted views
  • Browser history - Back/forward buttons work with search param changes
  • Type safety - Runtime validation and compile-time types
  • Persistence - State survives page refreshes
TanStack Router treats search params as first-class route state, not an afterthought.

Defining Search Parameters

Search parameters are defined using the validateSearch option on routes.

With Zod

The schema provides:
  • Validation - Invalid values are rejected
  • Defaults - Missing params get default values
  • Type inference - Full TypeScript types
  • Parsing - Strings converted to correct types

With Valibot

Manual Validation

For custom validation logic:
Manual validation gives you complete control but loses schema-based type inference.

Reading Search Parameters

Access search params using the useSearch hook.

Basic Usage

Typed Search Access

The search params are fully typed based on your schema:

With Selectors

Optimize re-renders by selecting specific fields:

Updating Search Parameters

Update search params through navigation.
Merge with current search params:
The search function receives previous search params and returns new ones.

Programmatic Updates

Clearing Search Params

Empty object removes all search params (defaults still apply).

Search Parameter Serialization

By default, TanStack Router uses JSON serialization for search params.

Default Serialization

The default implementation from packages/router-core/src/searchParams.ts:4-10:
This handles:
  • Primitives - strings, numbers, booleans
  • Arrays - ['tag1', 'tag2']?tags=["tag1","tag2"]
  • Objects - Nested object structures
  • null/undefined - Properly serialized

Custom Serialization

Provide custom serialization at the router level:
This enables serialization of Dates, Maps, Sets, and more.

Query String Format

TanStack Router uses the qss library for encoding:
Complex objects are JSON-stringified within the query string.

Search Param Inheritance

Child routes inherit search params from parents.

Parent Search Schema

At /posts/123?filter=react&commentId=5, both params are available:
  • Parent route sees { filter: 'react' }
  • Child route sees { filter: 'react', commentId: 5 }
The child schema is merged with the parent schema. Child schemas can redefine parent search params:

Search Middleware

Transform search parameters before they reach components.
Middleware runs after validation and before components receive search params.

Search Param Best Practices

Never trust search params from the URL. Always validate with a schema to ensure type safety and handle malformed URLs gracefully.
Use .default() in your schema for optional params. This makes URLs cleaner and components simpler.
When search params have a fixed set of valid values, use z.enum() to enforce them and get better type inference.
When updating filter/search params, reset page to 1 to avoid showing empty results.
Avoid deeply nested objects in search params. Flat structures create more shareable URLs.

Common Patterns

Pagination

Filtering

Sorting

Search with Debounce

Next Steps

Loaders

Learn how to use search params in data loading

Type Safety

Explore search parameter type inference

Navigation

Master search param navigation patterns

Caching

Understand how search params affect caching