-
-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return value of useQueryStates
does not immediately update after update in parsers
#760
Comments
What are you trying to achieve that requires switching the parser type for a given key at runtime? |
I want to make the type of parser and its default value dependent on other parameters,. For example the user might trigger a setting that changes the nature of a certain query param, e.g. from single to multiple values, or having a different default value. I can achieve this by sanitizing/parsing the values after the parsing by |
You can change the default value dynamically with a variable in
What would happen if a user with that setting set to single clicks on a URL set for multiple, and vice versa? This is to highlight that URLs are user-controlled, and that making their meaning as fully-qualified as possible (ie: not dependent on other internal application state) will make them much easier to reason about. I would suggest using different query keys, each mapping to an associated type, and using redirects when switching settings to rewrite a state key/value pair into another (you can use the serializer helper for this). |
You can set the default value dynamically, but only once. You cannot change the default value at runtime, based on external factors (see https://codesandbox.io/p/devbox/vkkl53)... In any case, thanks for your answer, and a great library in general. |
Ah, now this is indeed a bug with I'll take a look in #762, thanks! |
great, thanks so much. I'll take a look myself in the meantime as well, see if I can find the culprit... |
I think it has to do with the cache ref that's used for referential integrity, in useQueryState the default value is nullish-coalesced at the very end when returning the value, but in useQueryStates it's added to the cached state, and that isn't reactive to a change in the default value in the keyMap. |
I've got a fix that passes tests, though I'll run some more to see if it didn't break referential stability as a side-effect. Can you try the following please?
Please note that it only handles dynamic defaults, and not changing parser types at runtime. |
this seems to work! great job |
🎉 This issue has been resolved in version 2.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Context
What's your version of
nuqs
?"nuqs": "^2.1.2"
What framework are you using?
NextJS
Which version of your framework are you using?
15.0.3
Description
The return value of
useQueryStates
does not change if the parsers passed into it change.Reproduction
https://codesandbox.io/p/devbox/vkkl53
Example: Steps to reproduce the behavior:
-->
The text was updated successfully, but these errors were encountered: