queryKey undefined
Hi everyone! Does anyone know what the best approach is when queryKey might be undefined? Should I assign a random key even if I don't want the query to run or be cached? I'm trying to understand the reasoning behind it.
8 Replies
flat-fuchsia•3mo ago
always use descriptive queryKeys, you might want to reuse the same query data in different parts of your app, if you dont want the query to run, use
enabled: false
, if you dont want it cached, use gcTime: 0
rising-crimsonOP•3mo ago
but in this case that i am using a variable to define the queryKey what would be the solution, because i am defining a queryKey
return useQuery({
queryKey: ['exemple', product.value?.id],
enabled: Boolean(product.value))
},
})
flat-fuchsia•3mo ago
the above should be fine
rising-crimsonOP•3mo ago
so the queryKey like that is fine ?
flat-fuchsia•3mo ago
yeep, the query key will just be
['exemple', undefined]
which is fine
keep in mind that ['exemple', undefined]
is not always the same as ['exemple']
i cant check rn, but you could try the tanstack devtools to see if an entry is being added to the cacherising-crimsonOP•3mo ago
ohh ok because the documentation is saying that queryKey should have always a proper value, i was thinking if i sending a undefined could brake or be bad practice
flat-fuchsia•3mo ago
undefined in the queryKey is fine, you need to be aware that it is there though
they are also using this same method here:
https://tanstack.com/query/latest/docs/framework/vue/guides/dependent-queries#usequery-dependent-query
userId
can be undefined
, yet it is part of the queryKey
you can also have a read of this blog post by a maintainer, TkDodo:
https://tkdodo.eu/blog/effective-react-query-keys
I just now realized I am in #vue-query-questions, but the patterns are the same anywayrising-crimsonOP•3mo ago
Thankss a lot!! Definitely i will read it! Yh almost the same think, need to explore more the docs didnt saw that "Dependent Queries"