Please thoroughly read the Important Defaults before reading this guide
This caching example illustrates the story and lifecycle of:
Let's assume we are using the default cacheTime
of 5 minutes and the default staleTime
of 0
.
useQuery(['todos'], fetchTodos)
mounts.['todos']
query key, this query will show a hard loading state and make a network request to fetch the data.['todos']
key.staleTime
(defaults to 0
, or immediately).useQuery(['todos'], fetchTodos)
mounts elsewhere.['todos']
key from the first query, that data is immediately returned from the cache.fetchTodos
query functions are identical or not, both queries' status
are updated (including isFetching
, isLoading
, and other related values) because they have the same query key.['todos']
key is updated with the new data, and both instances are updated with the new data.useQuery(['todos'], fetchTodos)
query are unmounted and no longer in use.cacheTime
to delete and garbage collect the query (defaults to 5 minutes).useQuery(['todos'], fetchTodos)
mounts. The query immediately returns the available cached data while the fetchTodos
function is being run in the background. When it completes successfully, it will populate the cache with fresh data.useQuery(['todos'], fetchTodos)
unmounts.useQuery(['todos'], fetchTodos)
appear within 5 minutes.['todos']
key is deleted and garbage collected.The best JavaScript newsletter! Delivered every Monday to over 76,000 devs.