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

README.md

15 lines1.2 KB

1# TanStack Angular Query Pagination Example
2​
3Paginated projects with `placeholderData: keepPreviousData`, so the current page stays visible while the next one loads. The next page is prefetched.
4​
5## How it works
6​
7- `src/app/services/projects/queries.ts` is an `@Injectable` `ProjectsQueries` whose `list(page)` returns `queryOptions` keyed by `['projects', 'list', page]`, converting the `HttpClient` call from `src/app/services/projects/api.ts` with `lastValueFrom`.
8- `src/app/components/example.component.ts` spreads `list(this.page())` into `injectQuery` with `placeholderData: keepPreviousData` and a 5-second `staleTime`. While the next page loads, `data()` still holds the previous page and `isPlaceholderData()` is `true`, which disables the "Next Page" button.
9- An `effect` in `src/app/components/example.component.ts` fetches the next page ahead of time with `queryClient.query(this.#projectsQueries.list(newPage))` when the current page has `hasMore: true` and is not placeholder data.
10- `src/app/interceptor/projects-mock.interceptor.ts` is an `HttpInterceptorFn`, registered with `withInterceptors` in `src/app/app.config.ts`, that answers `/api/projects` with 10 projects per page after a 1-second delay.
11​
12To run this example:
13​
14- `npm install`
15- `npm start`