T
TanStack9mo ago
rival-black

If I set the queryFn to skipToken, can I omit the queryKey?

In the docs example:
const { data } = useQuery({
queryKey: ['todos', filter],
// ⬇️ disabled as long as the filter is undefined or empty
queryFn: filter ? () => fetchTodos(filter) : skipToken,
})
const { data } = useQuery({
queryKey: ['todos', filter],
// ⬇️ disabled as long as the filter is undefined or empty
queryFn: filter ? () => fetchTodos(filter) : skipToken,
})
when filter is undefined the queryKey becomes ['todos', undefined]. As far as I can tell this key will never result in a meaningful cache hit because data is never returned for this key. I would like to not have to generate a queryKey if I have empty arguments. Is this possible? I could set the queryKey to a dummy value like ['disabled'] but this could cause other problems if the same key is used elsewhere.
4 Replies
rare-sapphire
rare-sapphire9mo ago
queryKey: filter ? ['todos', filter] : ['todos']
rival-black
rival-blackOP9mo ago
Is there a particular reason you've chosen ['todos']? Could it be ['bananas']? The reason I ask is that I have a queryFactoryFunction that produces a valid queryKey and queryFunction or nothing at all. I'm unable to make a dummy queryKey in a typesafe way currently. I understand you have picked it because it helps you identify the query but functionally could it be anything and the behaviour would be the same?
rare-sapphire
rare-sapphire9mo ago
Could be anything
rival-black
rival-blackOP9mo ago
Ok, thanks

Did you find this page helpful?