-
-
Notifications
You must be signed in to change notification settings - Fork 134
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
useQueryStates updater function has an "old" value #763
Comments
Thanks for the report, there's indeed a cached value in there that failed to follow the link update. I'll see what I can do tomorrow. Interestingly, when clicking a link, I see two renders: one with the previous tab value, and one with the final one. Is that a Remix As an aside, you don't need to spread the previous value to update search params: only specify the ones you want to update, and they (should) be merged with the existing ones, so: onClick={() => {
setNuqsState((prev) => ({ ...prev, filters: '2' }))
}} can usually be simplified to: onClick={() => setNuqsState({ filters: '2' })} Now with this bug, there is still something that sticks that shouldn't, but this is what should be working eventually. |
Thanks for replying - I really like the api of this library! Just looking through the code for the remix adapter I don't think you need to use function useNuqsRemixAdapter() {
const [searchParams, setSearchParams] = useSearchParams()
const updateUrl = (search: URLSearchParams, options: AdapterOptions) => {
setSearchParams(search, {
replace: options.history === 'replace',
preventScrollReset: !options.scroll
})
};
return {
searchParams,
updateUrl
}
} I did have a quick look though and the double render still seems to remain with that change. I wander if its because of how the library works
^^ I guess the only way around that would be to rely solely on the remix for the url state changes i.e. no throttling / queuing etc (I suspect that built into remix already) - but that ruins the model of a common "implementation" used across "multiple frameworks" with an adapter pattern - which is super clean. Arguably remix overlaps quite a lot of what the library is aiming to achieve with the Something like this would perhaps be a more remix way... That would also get rid of the need for the high level context but I totally get its perhaps not the direction you want the library to go // NOTE - I haven't tested this - Its just an off the cuff what I think it might look like
function useQueryState(key, parser) {
const [params, setParams] = useSearchParams()
const setSearchParam = (nextValue) => {
const nextParams = new URLSearchParams(params)
nextParams.set(key, parser.stringify(nextValue))
setParams(nextParams, parser.options)
}
return [parser.parse(params.get(key)), setSearchParam]
}
const [state, setState] = useQueryState('count', parseAsInteger.withDefault(0).withOptions({
history: 'push',
shallow: true
})
) |
Regarding the rate-limiting, Remix uses the History API directly, and so is subject to the same issue of hitting errors when updating state too quickly: remix-rate-limits.movThe double render isn't surprising, as you said it's how nuqs works (optimistic updates, one for the local state and one when the URL has finished updating). It's the fact that the first render has the old value that seemed wrong, but it could be that I'm logging before it actually gets updated. Regarding encoding, Remix uses the same platform standards as the others: |
The original issue (stale value after navigation) could be solved by #776, could you try this and let me know if it fixes the issue on your end please? It does solve the issue in your reproduction repository.
|
🎉 This issue has been resolved in version 2.2.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Context
What's your version of
nuqs
?What framework are you using?
Which version of your framework are you using?
Description
When using the "state updater" function the "previous" value is out of date.
In the example the search params are updated using remix's
Link
component (which is a standard<a href="?tab=b">
when rendered)Reproduction
https://github.com/geemanjs/nuqs-remix-query-state-reproduction
Example: Steps to reproduce the behavior:
B
in the top row - these update the url using theLink
3
- these update the url with an updater functionA
(the default) when you click the numberIn the example if you use
nuqsState
instead of the updater function it works correctly. I'm guessing a reference somewhere isn't updated.The text was updated successfully, but these errors were encountered: