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

placeholder-query-data.md

63 lines1.4 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 result = 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 result = 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 // Until Angular supports signal-based inputs, we have to set a signal
40 @Input({ required: true, alias: 'postId' })
41 set _postId(value: number) {
42 this.postId.set(value)
43 }
44 postId = signal(0)
45 queryClient = inject(QueryClient)
46​
47 result = injectQuery(() => ({
48 queryKey: ['blogPost', this.postId()],
49 queryFn: () => fetch(`/blogPosts/${this.postId()}`),
50 placeholderData: () => {
51 // Use the smaller/preview version of the blogPost from the 'blogPosts'
52 // query as the placeholder data for this blogPost query
53 return queryClient
54 .getQueryData(['blogPosts'])
55 ?.find((d) => d.id === this.postId())
56 },
57 }))
59```
60​
61[//]: # 'ExampleCache'
62[//]: # 'Materials'
63[//]: # 'Materials'