DEV Community

Discussion on: Cleaner data fetching with react-query

Collapse
 
peteregbujie profile image
Peter Egbujie

Excellent post. So how can I access the cached data for a single post. Did you use useFetchProduct(id) or queryClient.getQueryData?

Collapse
 
siddharthvenkatesh profile image
Siddharth Venkatesh

You can use useFetchProduct(id) to get data for a single post. If the data is stale, react-query will fetch from your server. If the cache contains the fresh data, it will return it from the cache.
The "staleTime" property determines how long your data is valid. If you give its value as 10000, your data will be valid for 10 seconds. In this 10 seconds, whenever you call useFetchProduct(id), react-query will return the data from the cache.
Hope it clears things up.