📦 nuqs
@2
.0.0 is out ! 🚀
⚛️ Works with
@nextjs
14 & 15, but also
@reactjs
,
@remix_run
, react-router, and more
🧪 Testable: unit-test your components in isolation
🪶 20% smaller bundle size
Try it now: pnpm add nuqs
@latest
Fun fact: you can use the createSearchParamsCache feature of nuqs to parse and make dynamic segment slugs (route params) accessible to a page's RSC tree, without prop drilling, just like search params.
Don't stop at the name: it will parse & cache any { [key]: string } object.
📦 nuqs
@1
.20.0 is out! 🚀
- Remap useQueryStates keys: no more trade-off between shorter URLs & meaningful variables
- Serializer gets support for `null` to clear all managed search params from the base (h/t
@neefrehman_
)
📥 pnpm add nuqs
@latest
Coming soon to
@nuqs47ng
(like, tonight):
`useQueryStates` gets a new option to rename keys.
- Use variable names that make sense for your business logic
- Remap them to a different search param key
- Type-safe mapping, of course.
Super handy for shortening URLs.
📦 nuqs
@1
.19.0 is out!
- useQueryStates gets some DX love
- performance boost due to not running parsers when not needed
- referential stability for object/arrays states
Get it now:
Having a bit of fun: syncing the same React app with
@nuqs47ng
components, mounted twice.
One with the vanilla React adapter, one through React Router.
Even with "islands", everything is in sync, thanks to URL state.
@GreggLuskin
For SSR, it would be great to give the cache feature a validation argument where you could feed a Zod schema (or its parse function to make it validation-lib agnostic).
Would something like this work for you?
@jal_eelll
@shadcn
You might want to use a Date parser here:
For an improvement over the stock ones, check out what
@tremorlabs
did on their demo page, with a single key providing two [start, end] dates. 🧑🍳💋
We'll add some of these common custom parsers to the docs soon!
Next.js Conf is here. Join us in SF or online Oct 24.
• Sessions led by you (apply today)
• Hands-on workshops led by Next.js experts
• Meet and learn from the community
Register now and apply to speak:
@digitalconner
@nikelsnik
@nextjs
You can't use Context on the server, it's a client-only feature.
You could pass those props via Context to client components, but they already have access to it via useParams and useSearchParams.
This cache pattern bridges the gap by acting like a server-side context.
📦 nuqs
@2
.0.1 is out! 🚀
A couple of bug fixes after the big release:
- Fix
@nextjs
pages router import (next/navigation.js)
- Don't install all optionalDependencies (h/t
@nezouse
&
@AlemTuzlak
)
TIL: peerDependencies and peerDependenciesMeta.*.optional: true does the job.
@ty1erlaws0n
It's using React's `cache` function (in beta but available in Next.js) internally.
Credit for this pattern goes to
@ericbureltech
🙌
Source here:
@GreggLuskin
There's a discussion about Zod integration here:
TL;DR:
- Zod is great for validating deserialized state coming from search params.
- nuqs provides parsers & associated serializers to convert state to/from query string values
- Combining both is needed
@jal_eelll
@shadcn
The searchParamsCache won't work on client components, you'll need to split your definition this way:
export const searchParamsParsers = { ... }
export const searchParamsCache = createSearchParamsCache(searchParamsParsers)
Then in your client component, use searchParamsParsers.
@infodusha
@nikelsnik
@nextjs
Unfortunately Zod doesn't provide the serialisation needed when updating the URL. For simple types, the built-in `toString` might be enough.
Parsing a boolean also needs preprocessing in Zod, otherwise the string `"false"` being a truthy value returns `true` as a boolean.
@GreggLuskin
To be fair, the pattern could be adapted to be completely agnostic of nuqs, and be shipped in a separate package.
That would allow it to work for params, search params and any Record<string, string> type.
@nikelsnik
Tip: don't forget the `as const` after the literal option arrays.
Otherwise it broadens the type to `string` or `number`, rather than `"graphite" | "sage"` or `2 | 4 | 6`.