Dynamic segments of the queryKey are undefined on page refresh and initial mount
What is the best way to handle this...? With Vue, I put the queryKey value inside a ref and I use watchEffect to update it and use refetch to fetch data for the first time to have it registered under the right queryKey
This works fine, but I was wondering if there is a way the undefined queryKey (
/carts/undefined
) could be avoided. Cheers and thanks! ❤️10 Replies
crude-lavender•2y ago
Replace ref with computed and pass it to enabled and queryKey. This will also allow to remove watchEffect/refetch completely.
crude-lavender•2y ago
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
crude-lavender•2y ago
when cartId changes queryKey will update and you effectively get a new query.
vue-query
will then refetch data if it's requiredvicious-goldOP•2y ago
Excellent! This looks much cleaner, thanks!
vicious-goldOP•2y ago
I just had a chance to give your solution a shot and this is exactly what happened 🥳 No duplicate network requests, no data appended to a key with "undefined", just a single empty key with null

vicious-goldOP•2y ago
so I suppose then that my mistake was to put a single interpolated string inside the query key array, it should have been an array of variables instead?
crude-lavender•2y ago
I can recommend this article https://tkdodo.eu/blog/effective-react-query-keys. It covers why queryKey is an array and how to define your query keys for better results.
from your screenshot i can recommend updating queryKey for profiles as well
vicious-goldOP•2y ago
oh that's great
crude-lavender•2y ago
will make it easier to invalidate and just let vue-query do it's job 🙂
vicious-goldOP•2y ago
ah, well spotted, will definitely have to fix that 👍