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

6 messages · Page 1 of 1 (latest)

olive fog
#

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,
  })

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.

sleek trench
#

queryKey: filter ? ['todos', filter] : ['todos']

olive fog
#

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?

sleek trench
#

Could be anything