Open History to see the last commit
1---
2id: placeholder-query-data
3title: Placeholder Query Data
4ref: docs/framework/react/guides/placeholder-query-data.md
5---
6β
7[//]: # 'ExampleValue'
8β
9```ts
10class TodosComponent {
11 readonly todosQuery = injectQuery(() => ({
12 queryKey: ['todos'],
13 queryFn: () => fetch('/todos'),
14 placeholderData: placeholderTodos,
15 }))
16}
17```
18β
19[//]: # 'ExampleValue'
20[//]: # 'Memoization'
21[//]: # 'Memoization'
22[//]: # 'ExampleFunction'
23β
24```ts
25class TodosComponent {
26 readonly todoQuery = injectQuery(() => ({
27 queryKey: ['todos', id()],
28 queryFn: () => fetch(`/todos/${id}`),
29 placeholderData: (previousData, previousQuery) => previousData,
30 }))
31}
32```
33β
34[//]: # 'ExampleFunction'
35[//]: # 'ExampleCache'
36β
37```ts
38export class BlogPostComponent {
39 postId = input.required<number>()
40 readonly queryClient = inject(QueryClient)
41β
42 readonly blogPostQuery = injectQuery(() => ({
43 queryKey: ['blogPost', this.postId()],
44 queryFn: () => fetch(`/blogPosts/${this.postId()}`),
45 placeholderData: () => {
46 // Use the smaller/preview version of the blogPost from the 'blogPosts'
47 // query as the placeholder data for this blogPost query
48 return this.queryClient
49 .getQueryData(['blogPosts'])
50 ?.find((d) => d.id === this.postId())
51 },
52 }))
53}
54```
55β
56[//]: # 'ExampleCache'
57[//]: # 'Materials'
58[//]: # 'Materials'