Difference between a query function returning undefined and a query function throwing an error
I had expected after v4 that returning undefined has the same behavior as throwing an error since undefined won't get added to the cache but in practice it seems like returning an error results in the query function being called far more often. What exactly is happening here?
7 Replies
fascinating-indigoOP•16mo ago
ah throwing an error must trigger retries
but would e.g. undefined trigger the retry on mount?
so undefined results in an error status and doesn't update the cache but isn't treated like a thrown error in terms of retries
national-gold•16mo ago
Migrating to React Query 4 | TanStack Query React Docs
Breaking Changes
v4 is a major version, so there are some breaking changes to be aware of:
national-gold•16mo ago
Make sure you don't return
undefined
in the queryFn
national-gold•16mo ago
Also, the latest docs repeat this for the last bullet of
queryFn
: https://tanstack.com/query/latest/docs/framework/react/reference/useQuery
Must return a promise that will either resolve data or throw an error. The data cannot be undefined.
useQuery | TanStack Query React Docs
const {
data,
fascinating-indigoOP•16mo ago
sure, you're not supposed to but you can and nothing particularly bad happens
for the purposes of a large migration it might be sane to let old functions keep returning undefined
sunny-green•16mo ago
We cannot store undefined and keep the query in success state. I think we warn in dev mode and go to error state. Not sure why we don't retry but it's really an invariant
Ah we would need to move the check to the retryer. But retries don't help when you return undefined 🤷♂️
fascinating-indigoOP•16mo ago
right I think it's fine for undefined to be an error state