Document vue-query's reactivity #5912
-
Currently vue-query's part of tanstack completely lacks documentation on it's relations with vue reactivity (unless I'm missing it somehow). There are some ts types, but it's very hard to understand what vue-query actually does to support reactivity. Sources also doesn't particularly scream the idea into your face. Even worse, vue's best practices for composables doesn't seem to be followed? For example, say you want to make a composable returning a query, super common IMO: export const useUserQuery = (id: MaybeRefOrGetter<number>) => useQuery({ // MaybeRefOrGetter as vue docs tell
queryKey: ['user', ???], // id? toValue(id)? unref(id)? toRef(id)? something else?
queryFn: async ({queryKey: [_, id]}) => await yourRequest(???) // again, id? toValue(id)? unref(id)? toRef(id)? something else?
});
// wrap it all into useEffect? verbose, stupid and ugly Something works and something does not, e.g. getters doesn't seem to?.. Very confusing. And maybe that's completely wrong way to use vue-query, but the problem is intended way is not explained anywhere. Please ignore if I'm the only idiot struggling with it. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I agree that such page might be handy. Especially for people moving from other frameworks. As a rule of thumb, everything that you are passing to As for the |
Beta Was this translation helpful? Give feedback.
-
@KazimirPodolski I also encountered lack of documentation, sometimes I look through react documentation to find some info. But key reactivity works by You can preserve reactivity if you pass ref, computed. |
Beta Was this translation helpful? Give feedback.
I agree that such page might be handy. Especially for people moving from other frameworks.
As a rule of thumb, everything that you are passing to
useQuery
options should bereactive
/ref
, so library will be aware of the changes of those values. Vue-Query will unwrap them internally.As for the
queryFn
- it's a plain JS function out of control of vue-query, so you have to be aware of what you are using.id
that you are getting from thequeryFn context
is in unwrapped state already, so can be used as-is. But if you are referencingref
not viacontext
, you need to unwrap it manually.Typescript should help you with that, hinting what is what.