Make invalidateQueries not instant refetch but wait for next mount
Hi guys,
I am having some issues with invalidateQueries.
I use react query to fetch a list of elements that I dispatch in a global state. I would like to use the cache as much as possible.
When an item is deleted I am deleting it from the global state and I send a request to delete it in DB.
At this point I do not need or want to refetch.
Note that next time my component (with its own query key) will be mounted the global state won’t be 100% trustable.
This is why I would like to invalidate the query on item deletion. And refetch the list on mount, not before.
So, so far I am using invalidateQueries at deletion. Issue is that:
invalidateQueries([myKey]);
-> trigger a refetch
invalidateQueries([myKey], {
refetchType: 'none',
});
-> Do not refetch on mount
I tried some stuff and looked online but didn’t found anything, am I missing something?
Thanks
6 Replies
adverse-sapphire•4y ago
refetchType: none should be what you want. it marks the query as stale but doesn't immediately refetch it.
the next time a mount happens, it should be refefetched.fascinating-indigo•4y ago
My app dosen't know refetchType: none
adverse-sapphire•4y ago
Are you on version 4 of react-query?
fascinating-indigo•4y ago
3.38
v4 is still beta or?
conscious-sapphireOP•4y ago
Well, it seems my issues were because I yarn add "react-query" instead of "@tanstack/react-query"... , working as expected now, thanks anyway for your help
adverse-sapphire•4y ago
v4 is not in beta, no