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```tsx
10function Todos() {
11 const todosQuery = useQuery(() => ({
12 queryKey: ['todos'],
13 queryFn: () => fetch('/todos'),
14 placeholderData: placeholderTodos,
15 }))
16}
17```
18β
19[//]: # 'ExampleValue'
20[//]: # 'Memoization'
21β
22### Placeholder Data Memoization
23β
24If the process for accessing a query's placeholder data is intensive or just not something you want to perform on every render, you can memoize the value:
25β
26```tsx
27function Todos() {
28 const placeholderData = createMemo(() => generateFakeTodos())
29 const todosQuery = useQuery(() => ({
30 queryKey: ['todos'],
31 queryFn: () => fetch('/todos'),
32 placeholderData: placeholderData(),
33 }))
34}
35```
36β
37[//]: # 'Memoization'
38[//]: # 'ExampleFunction'
39β
40```tsx
41const todosQuery = useQuery(() => ({
42 queryKey: ['todos', id],
43 queryFn: () => fetch(`/todos/${id}`),
44 placeholderData: (previousData, previousQuery) => previousData,
45}))
46```
47β
48[//]: # 'ExampleFunction'
49[//]: # 'ExampleCache'
50β
51```tsx
52function BlogPost(props) {
53 const queryClient = useQueryClient()
54 const blogPostQuery = useQuery(() => ({
55 queryKey: ['blogPost', props.blogPostId],
56 queryFn: () => fetch(`/blogPosts/${props.blogPostId}`),
57 placeholderData: () => {
58 // Use the smaller/preview version of the blogPost from the 'blogPosts'
59 // query as the placeholder data for this blogPost query
60 return queryClient
61 .getQueryData(['blogPosts'])
62 ?.find((d) => d.id === props.blogPostId)
63 },
64 }))
65}
66```
67β
68[//]: # 'ExampleCache'