本页还没有译文
本页 7 段还没译成日本語,下面显示的是英文原文。Caching Examples
还没翻译译自英文(tanstack.com)本页 7 段都还没译
Please thoroughly read the Important Defaults before reading this guide
Basic Example
This caching example illustrates the story and lifecycle of:
- Query Instances with and without cache data
- Background Refetching
- Inactive Queries
- Garbage Collection
Let's assume we are using the default gcTime of 5 minutes and the default staleTime of 0.
- A new instance of
useQuery({ queryKey: ['todos'], queryFn: fetchTodos })mounts.- Since no other queries have been made with the
['todos']query key, this query will show a hard loading state and make a network request to fetch the data. - When the network request has completed, the returned data will be cached under the
['todos']key. - The data will be marked as stale after the configured
staleTime(defaults to0, or immediately).
- Since no other queries have been made with the
- A second instance of
useQuery({ queryKey: ['todos'], queryFn: fetchTodos })mounts elsewhere.- Since the cache already has data for the
['todos']key from the first query, that data is immediately returned from the cache. - The new instance triggers a new network request using its query function.
- Note that regardless of whether both
fetchTodosquery functions are identical or not, both queries'statusare updated (includingisFetching,isPending, and other related values) because they have the same query key.
- Note that regardless of whether both
- When the request completes successfully, the cache's data under the
['todos']key is updated with the new data, and both instances are updated with the new data.
- Since the cache already has data for the
- Both instances of the
useQuery({ queryKey: ['todos'], queryFn: fetchTodos })query are unmounted and no longer in use.- Since there are no more active instances of this query, a garbage collection timeout is set using
gcTimeto delete and garbage collect the query (defaults to 5 minutes).
- Since there are no more active instances of this query, a garbage collection timeout is set using
- Before the cache timeout (gcTime) has completed, another instance of
useQuery({ queryKey: ['todos'], queryFn: fetchTodos })mounts. The query immediately returns the available cached data while thefetchTodosfunction is being run in the background. When it completes successfully, it will populate the cache with fresh data. - The final instance of
useQuery({ queryKey: ['todos'], queryFn: fetchTodos })unmounts. - No more instances of
useQuery({ queryKey: ['todos'], queryFn: fetchTodos })appear within 5 minutes.- The cached data under the
['todos']key is deleted and garbage collected.
- The cached data under the
AI 翻译查看英文原文