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