TanStackT
TanStack6mo ago
18 replies
sad-indigo

`enabled: false` returning cached data

Hi there! We've noticed a bit of a footgun in our codebase. Folks on our team will use
useQuery
and pass in a query object that depends on some data. If that data isn't available or isn't loaded yet, they'll set
enabled: false
to disable the query. However, disabling a query only prevents the query from running, and it may still return cached data (see https://tanstack.com/query/latest/docs/framework/react/guides/disabling-queries). People then write the rest of the code expecting data to be undefined, but in prod that data will sometimes happen to be cached by some other part of the app and users will get all of that cached data instead of undefined and the UI will break or behave unexpectedly.

Our solution was to overwrite
useQuery
to have the resulting data always be undefined if enabled is false since that's what people on our team expect to happen. Is there any downside to doing this?

I've attached our overwritten
useQuery
with what I believe are the correct types (I copy-pasted most of the code from the
useQuery
implementation). I don't love having to use useMemo but it seemed preferable to updating
data
directly on the returned object.
Was this page helpful?