Server-Side usage
Type-safe search params on the server
This feature is available for Next.js only.
If you wish to access the searchParams in a deeply nested Server Component
(ie: not in the Page component), you can use createSearchParamsCache
to do so in a type-safe manner.
Note
Parsers don’t validate your data. If you expect positive integers or JSON-encoded objects of a particular shape, you’ll need to feed the result of the parser to a schema validation library, like Zod.
Built-in validation support is coming. Read the RFC
The cache will only be valid for the current page render
(see React’s cache
function).
Note: the cache only works for server components, but you may share your
parser declaration with useQueryStates
for type-safety in client components:
Shorter search params keys
Just like useQueryStates
, you can
define a urlKeys
object to map the variable names defined by the parser to
shorter keys in the URL. They will be translated on read and your codebase
can only refer to variable names that make sense for your domain or business logic.