-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Closed
Milestone
Description
Please see this discussion
This is an ever-recurring issue that you can't bail out of updates with setQueryData
. But because the updater function gives you a potentially undefined value (if there is no cache entry for the key yet), TypeScript is very unhappy because you're supposed to create a new cache entry, and there is no "bailing out of it" because react-query actually creates the cache entry before the functional updater of setQueryData is called.
Proposed solution
As stated here:
- let's allow the setQueryData functional updater to return undefined
- signature: T | ((prev: T | undefined) => T | undefined)
- if
undefined
is returned, we do not create or update the cache entry - that means if we have a previous value and
undefined
is returned -undefined
is not written to the cache, and observers are not informed about that. - this is technically breaking if
undefined
is a legit cache entry value for someone
This would be in-ine with what the initialData
function already does: if undefined
is returned from that, it is not persisted to the cache.
Also, if a fetch function returns undefined
, we should:
- transform it to a failed promise (error)
- warn during development mode about it
neongreen