Allow serialize
functions to operate on ReadOnlyUrlSearchParams
from useSearchParams
#646
-
We often have a need to create href's to update search params, without affecting any other current params on the page. For example, one link might open a modal, but we want to preserve the underlying pagination state of a table. To do this, it feels most ergonomic to pass in the response from // We don't want to define pagination state here as it is not necessarily a concern of the current component
const serializer = createSerializer({ objectToOpenInModal: parseAsString })
...
const currentSearchParams = useSearchParams()
...
<Link href={serializer(currentSearchParams, { objectToOpenInModal: id })}>Open modal</Link> But, this doesn't work and gives the following Next.js error: <Link href={serializer(new URLSearchParams(currentSearchParams), { objectToOpenInModal: id })}>Open modal</Link> But this feels like a bit too much boilerplate for a library that is deeply integrated with Next, so I wonder if it should be something that the serializer's implementation could be responsible for? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
That's a good idea! Regarding the "tight integration with Next.js", as an FYI this is kind of going away with the upcoming Adapters feature in #644. That being said, it looks like the error could be solved by working on a copy of the nuqs/packages/nuqs/src/serializer.ts Lines 73 to 75 in 1049f96 Would you like to open a PR? |
Beta Was this translation helpful? Give feedback.
Thanks for the pointer! I've just opened a PR in #647