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

placeholder-query-data.md

58 lines1.3 KB

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 }))
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 }))
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 }))
54```
55​
56[//]: # 'ExampleCache'
57[//]: # 'Materials'
58[//]: # 'Materials'