React-query: useQuery executes queryFn with falsy queryKey

Created on 18 Aug 2020  路  4Comments  路  Source: tannerlinsley/react-query

useQuery executes a fetch function even though the query key is a falsy value.
I confirmed this behavior in latest version 2.5.14.
(My previos sample with version 1.2.1, useQuery doesn't fetch with falsy query key as described in the official docs.)

See my codesandbox.
https://codesandbox.io/s/falsy-querykey-mce4s?file=/src/App.tsx

useQuery-falsyKey

Most helpful comment

I'll update the docs :)

All 4 comments

Hey @oukayuka, I've been passing { enabled: false } whenever I want to do the conditional fetching, but curious to know the reason why we want to have the query key as null.

@chriszhangusc Yes, I know that and now I also use useQuery like this.

const { data: users = [] } = useQuery(
  [orgCode, 'members'], (code) => getMembers(code), { enabled: !!orgCode },
);

But API Reference says,
https://react-query.tanstack.com/docs/api#usequery

Options

  • queryKey: String | [String, ...any] | falsy

I don't know when the specs changed, but if this is the correct behavior, shouldn't this | falsy be removed?
I assume many developers to think the query won't be executed when key is falsy, and actually previous versions and SWR do that.

@oukayuka Ohh you are right! Looks like a legit bug. Thanks for the clarification!

I'll update the docs :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GuillaumeSpera picture GuillaumeSpera  路  5Comments

michaeljota picture michaeljota  路  6Comments

bgazzera picture bgazzera  路  4Comments

benediktviebahn picture benediktviebahn  路  4Comments

the133448 picture the133448  路  4Comments