Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

initial-query-data.md

127 lines3.0 KB

1---
2id: initial-query-data
3title: Initial Query Data
4ref: docs/framework/react/guides/initial-query-data.md
5replace:
6 {
7 '@tanstack/react-query': '@tanstack/solid-query',
8 'useMutationState[(]': 'useMutationState(() => ',
9 'useMutation[(]': 'useMutation(() => ',
10 'useQuery[(]': 'useQuery(() => ',
11 'useQueries[(]': 'useQueries(() => ',
12 'useInfiniteQuery[(]': 'useInfiniteQuery(() => ',
13 }
14---
15​
16[//]: # 'Example'
17​
18```tsx
19const todosQuery = useQuery(() => ({
20 queryKey: ['todos'],
21 queryFn: () => fetch('/todos'),
22 initialData: initialTodos,
23}))
24```
25​
26[//]: # 'Example'
27[//]: # 'Example2'
28​
29```tsx
30// Will show initialTodos immediately, but also immediately refetch todos after mount
31const todosQuery = useQuery(() => ({
32 queryKey: ['todos'],
33 queryFn: () => fetch('/todos'),
34 initialData: initialTodos,
35}))
36```
37​
38[//]: # 'Example2'
39[//]: # 'Example3'
40​
41```tsx
42// Show initialTodos immediately, but won't refetch until another interaction event is encountered after 1000 ms
43const todosQuery = useQuery(() => ({
44 queryKey: ['todos'],
45 queryFn: () => fetch('/todos'),
46 initialData: initialTodos,
47 staleTime: 1000,
48}))
49```
50​
51[//]: # 'Example3'
52[//]: # 'Example4'
53​
54```tsx
55// Show initialTodos immediately, but won't refetch until another interaction event is encountered after 1000 ms
56const todosQuery = useQuery(() => ({
57 queryKey: ['todos'],
58 queryFn: () => fetch('/todos'),
59 initialData: initialTodos,
60 staleTime: 60 * 1000, // 1 minute
61 // This could be 10 seconds ago or 10 minutes ago
62 initialDataUpdatedAt: initialTodosUpdatedTimestamp, // eg. 1608412420052
63}))
64```
65​
66[//]: # 'Example4'
67[//]: # 'Example5'
68​
69```tsx
70const todosQuery = useQuery(() => ({
71 queryKey: ['todos'],
72 queryFn: () => fetch('/todos'),
73 initialData: () => getExpensiveTodos(),
74}))
75```
76​
77[//]: # 'Example5'
78[//]: # 'Example6'
79​
80```tsx
81const todoQuery = useQuery(() => ({
82 queryKey: ['todo', todoId],
83 queryFn: () => fetch('/todos'),
84 initialData: () => {
85 // Use a todo from the 'todos' query as the initial data for this todo query
86 return queryClient.getQueryData(['todos'])?.find((d) => d.id === todoId)
87 },
88}))
89```
90​
91[//]: # 'Example6'
92[//]: # 'Example7'
93​
94```tsx
95const todoQuery = useQuery(() => ({
96 queryKey: ['todos', todoId],
97 queryFn: () => fetch(`/todos/${todoId}`),
98 initialData: () =>
99 queryClient.getQueryData(['todos'])?.find((d) => d.id === todoId),
100 initialDataUpdatedAt: () =>
101 queryClient.getQueryState(['todos'])?.dataUpdatedAt,
102}))
103```
104​
105[//]: # 'Example7'
106[//]: # 'Example8'
107​
108```tsx
109const todoQuery = useQuery(() => ({
110 queryKey: ['todo', todoId],
111 queryFn: () => fetch(`/todos/${todoId}`),
112 initialData: () => {
113 // Get the query state
114 const state = queryClient.getQueryState(['todos'])
115​
116 // If the query exists and has data that is no older than 10 seconds...
117 if (state && Date.now() - state.dataUpdatedAt <= 10 * 1000) {
118 // return the individual todo
119 return state.data.find((d) => d.id === todoId)
120 }
121​
122 // Otherwise, return undefined and let it fetch from a hard loading state!
123 },
124}))
125```
126​
127[//]: # 'Example8'