T
TanStack3w ago
sensitive-blue

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
genetic-orange
genetic-orange3w 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
sensitive-blue
sensitive-blueOP3w 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)) }, })
genetic-orange
genetic-orange3w ago
the above should be fine
sensitive-blue
sensitive-blueOP3w ago
so the queryKey like that is fine ?
genetic-orange
genetic-orange3w 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 cache
sensitive-blue
sensitive-blueOP3w 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
genetic-orange
genetic-orange3w 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 anyway
sensitive-blue
sensitive-blueOP3w 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"

Did you find this page helpful?