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

paginated-queries.md

113 lines3.0 KB

1---
2id: paginated-queries
3title: Paginated / Lagged Queries
4ref: docs/framework/react/guides/paginated-queries.md
5replace:
6 {
7 'useQuery': 'injectQuery',
8 'useInfiniteQuery': 'injectInfiniteQuery',
9 'hook': 'function',
10 }
11---
12​
13[//]: # 'Example'
14​
15```ts
16const projectsQuery = injectQuery(() => ({
17 queryKey: ['projects', page()],
18 queryFn: fetchProjects,
19}))
20```
21​
22[//]: # 'Example'
23[//]: # 'Example2'
24​
25```angular-ts
26@Component({
27 selector: 'pagination-example',
28 template: `
29 <div>
30 <p>
31 In this example, each page of data remains visible as the next page is
32 fetched. The buttons and capability to proceed to the next page are also
33 suppressed until the next page cursor is known. Each page is cached as a
34 normal query too, so when going to previous pages, you'll see them
35 instantaneously while they are also re-fetched invisibly in the
36 background.
37 </p>
38 @if (projectsQuery.status() === 'pending') {
39 <div>Loading...</div>
40 } @else if (projectsQuery.status() === 'error') {
41 <div>Error: {{ projectsQuery.error().message }}</div>
42 } @else {
43 <!-- 'data' will either resolve to the latest page's data -->
44 <!-- or if fetching a new page, the last successful page's data -->
45 <div>
46 @for (project of projectsQuery.data().projects; track project.id) {
47 <p>{{ project.name }}</p>
48 }
49 </div>
50 }
51​
52 <div>Current Page: {{ page() + 1 }}</div>
53 <button (click)="previousPage()" [disabled]="page() === 0">
54 Previous Page
55 </button>
56 <button
57 (click)="nextPage()"
58 [disabled]="
59 projectsQuery.isPlaceholderData() || !projectsQuery.data()?.hasMore
60 "
61 >
62 Next Page
63 </button>
64 <!-- Since the last page's data potentially sticks around between page requests, -->
65 <!-- we can use 'isFetching' to show a background loading -->
66 <!-- indicator since our status === 'pending' state won't be triggered -->
67 @if (projectsQuery.isFetching()) {
68 <span> Loading...</span>
69 }
70 </div>
71 `,
72})
73export class PaginationExampleComponent {
74 page = signal(0)
75 readonly #queryClient = inject(QueryClient)
76​
77 readonly projectsQuery = injectQuery(() => ({
78 queryKey: ['projects', this.page()],
79 queryFn: () => lastValueFrom(fetchProjects(this.page())),
80 placeholderData: keepPreviousData,
81 staleTime: 5 * 1000,
82 }))
83​
84 constructor() {
85 effect(() => {
86 // Prefetch the next page!
87 if (
88 !this.projectsQuery.isPlaceholderData() &&
89 this.projectsQuery.data()?.hasMore
90 ) {
91 void this.#queryClient
92 .query({
93 queryKey: ['projects', this.page() + 1],
94 queryFn: () => lastValueFrom(fetchProjects(this.page() + 1)),
95 })
96 .catch(noop)
97 }
98 })
99 }
100​
101 previousPage() {
102 this.page.update((old) => Math.max(old - 1, 0))
103 }
104​
105 nextPage() {
106 this.page.update((old) =>
107 this.projectsQuery.data()?.hasMore ? old + 1 : old,
108 )
109 }
111```
112​
113[//]: # 'Example2'