How can I refetch on a useQuery() in composable as its outside of script setup?
I am having issues where something has happened in my app and I need to refetch a specific query, but where the logic of something happening is in a composable which is looped back on itself (polling server for something) which means it is executing outside of script setup.
Is there a way I can do this outside of script setup?

2 Replies
other-emeraldOP•3y ago
Interesting, don't know why this is the case but if inside my composable I destructure refresh here and then I can call that function inside the composable, but if i just call the func without destructuring it says i cant do it outside of script setupp
I think the difference here between my other exact same structured composables is that the functions are firing from a @click inside a vue component, whereas here the function im refetching in, is called by another function in the composable, thus not directly from the vue component/script setup
unwilling-turquoise•3y ago
useQuery
returns refetch
that you can use to forcefully refetch query.
You should also use useQuery
only directly in setup
, not in callbacks cause of the component lifecycle. Observers that are being created might not be cleaned up if it's run in the callback.
Also provide/inject flow only works directly in setup.
There is an escape hatch where you can get the queryClient
via useQueryClient
in setup and then pass it to useQuery
. Then query will use this provided instance instead of looking for it in the component context.
However I do not advise to use that unless you know what you are doing, and potentially you should create scope for it in that case.