T
TanStack3y ago
stormy-gold

How to reset data on component remount

When component mounts first time its data is undefined, then after fetch is completed it is lets say "data1". After component is remounted, I want data field to be again undefined but getting "data1"(from previous fetch) until new fetch is completed and it is "data2". How can I reset data? P.S. cache and stale time is already 0
2 Replies
stormy-gold
stormy-goldOP3y ago
okay, i figured out, that I can use resetQueries() on client, but is there any simple way to do it for a single query without client.resetQuery(key)?
generous-apricot
generous-apricot3y ago
react query is built upon the stale while revalidate caching mechanism, which is to prefer stale data ("data1" in your example) to not showing anything at all or a loading spinner while new data ("data2") is being fetched. that said, you can just check the isFetchedAfterMount flag returned from useQuery and render a spinner while this is false

Did you find this page helpful?