Utilities
Utilities for working with query strings
Serializer helper
To populate <Link>
components with state values, you can use the createSerializer
helper.
Pass it an object describing your search params, and it will give you a function to call with values, that generates a query string serialized as the hooks would do.
Example:
Base parameter
The returned serialize
function can take a base parameter over which to
append/amend the search params:
Shorter search params keys
Just like useQueryStates
, you can
specify a urlKeys
object to map the variable names defined by the parsers
to shorter keys in the URL:
Parser type inference
To access the underlying type returned by a parser, you can use the
inferParserType
type helper:
For an object describing parsers (that you’d pass to createSearchParamsCache
or to useQueryStates
), inferParserType
will
return the type of the object with the parsers replaced by their inferred types:
Standard Schema
Search param definitions can be turned into a Standard Schema for validating external sources and passing on type inference to other tools.
TanStack Router & validateSearch
Note
TanStack Router support is still experimental, see PR #953 for progress and feedback 🙏
You can pass the standard schema validator to
TanStack Router’s validateSearch
for type-safe
linking to nuqs URL state, but in order to keep those
values optional (as nuqs uses different defaults strategies
than TSR), you need to mark the output as Partial
,
using the partialOutput
option: