-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Vue - Query key doesn't match the current value of ref used inside #5910
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
Comments
Yes, you are correct. Do you want to provide a fix for it? |
Additionally, the following code may resolve your issue: <script setup lang="ts">
import { useQuery } from '@tanstack/vue-query';
import { onMounted, ref } from 'vue';
const id = ref(0);
const isMounted = ref(false);
const { data } = useQuery({
queryKey: ['multiply', id] as const,
queryFn: async ({queryKey}) => {
console.log("Current value of ref:", id.value);
console.log("Current value of query key:", queryKey[1]);
return Promise.resolve(id.value * 2)
},
enabled: isMounted
});
onMounted(() => isMounted.value = true);
async function increment() {
console.log('Increment clicked');
id.value++;
}
</script> |
Hi @DamianOsipiuk, I tried and got as far as fixing my issue. But unfortunately it looks like adding flush sync to the watchers breaks dependent queries when providing a computed to the enabled property. |
Thanks, it does solve the issue but also causes the queryFn to be triggered twice which is not exactly what I want. :) |
Looking forward to your PR. Additionally, 2023-08-28.7.10.04.mov |
@Mini-ghost you're right, my bad. I probably still had the refetch in there. But without it, the code won't longer have the desired behavior as it would now be triggered by a change of the key. But my goal is to only run the query when manually calling refetch. |
I looked at the content you forked from this repository; perhaps it could solve the problem. Additionally until the issue is resolved, the above-mentioned method maybe can be considered as a temporary alternative solution. |
Describe the bug
Hi,
I was trying to build a filter for some data using tanstack query.
It's supposed to only refetch the data with the given values whenever the user clicks a button so not automatically.
Therfore I set enabled to false to only execute the query when manually calling
refetch()
.When testing it I encountered some wired behavior: The query key seems to always lack behind one iteration from the actual value of my filter.
I created a minimal example of the issue to make it reproducable and easier to explain:
In the above example I want to update data with the new value of id multiplied by two whenever the Increment button is clicked. (This could of course be done without manually calling
refetch()
but I needed a simple example)Whenever the increment button is clicked the id gets incremented correctly but the query used during the refetch will still use the old value of id. Therfore the result is assigned to the old key:
Meanwhile data is always undefined as the key changes right after the data was fetched.
If I manually await the next tick inside vues' reactivity system the value is updated correctly before the refetch is triggered and everything works as expected.
That's why I guess it could be something related to the reactivity chain of the queryKey. Maybe a lazily evaluated computed or something similar.
Your minimal, reproducible example
https://github.com/markbrockhoff/tanstack-query-bug-repro
Steps to reproduce
pnpm i
pnpm dev
Expected behavior
I'd expect the changes to reactive parts of the queryKey to take affect immediately so the refetch uses the correct queryKey without manually awaiting the nextTick before triggering it.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
Tanstack Query adapter
vue-query
TanStack Query version
v4.34.0
TypeScript version
5.2.2
Additional context
No response
The text was updated successfully, but these errors were encountered: