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
Defining Search Parameters
Search parameters are defined using thevalidateSearch option on routes.
With Zod
- 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:Reading Search Parameters
Access search params using theuseSearch 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.With Link Component
Updating Existing Search
Merge with current search params:search function receives previous search params and returns new ones.
Programmatic Updates
Clearing Search Params
Search Parameter Serialization
By default, TanStack Router uses JSON serialization for search params.Default Serialization
The default implementation frompackages/router-core/src/searchParams.ts:4-10:
- 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:Query String Format
TanStack Router uses theqss library for encoding:
Search Param Inheritance
Child routes inherit search params from parents.Parent Search Schema
/posts/123?filter=react&commentId=5, both params are available:
- Parent route sees
{ filter: 'react' } - Child route sees
{ filter: 'react', commentId: 5 }
Overriding Parent Search
Child schemas can redefine parent search params:Search Middleware
Transform search parameters before they reach components.Search Param Best Practices
Always validate search params
Always validate search params
Never trust search params from the URL. Always validate with a schema to ensure type safety and handle malformed URLs gracefully.
Provide sensible defaults
Provide sensible defaults
Use
.default() in your schema for optional params. This makes URLs cleaner and components simpler.Use enums for controlled values
Use enums for controlled values
When search params have a fixed set of valid values, use
z.enum() to enforce them and get better type inference.Reset pagination when filtering
Reset pagination when filtering
When updating filter/search params, reset
page to 1 to avoid showing empty results.Keep URLs readable
Keep URLs readable
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