Skip to content

Commit ac3b76e

Browse files
committed
Document infinite query display
1 parent 79a4295 commit ac3b76e

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

docs/rtk-query/usage/infinite-queries.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ Infinite query hooks return [the same result object as normal query hooks](./que
187187

188188
In most cases, you will probably read `data` and either `isLoading` or `isFetching` in order to render your UI. You will also want to use the `fetchNext/PreviousPage` methods to trigger fetching additional pages.
189189

190+
### Displaying Infinite Query Data
191+
192+
For infinite query hooks, the `data` field returned by the hook will be the `{pages, pageParams}` structure containing all fetched pages, instead of just a single response value.
193+
194+
This gives you control over how the data is used for display. You can flatten all the page contents into a single array for infinite scrolling, use the individual page results for pagination, sort or reverse the entries, or any other logic you need for rendering the UI with this data.
195+
196+
As with any other Redux data, you should avoid mutating these arrays (including calling `array.sort/reverse()` directly on the existing references).
197+
190198
### Infinite Query Hook Usage Example
191199

192200
Here is an example of a typical infinite query endpoint definition, and hook usage in a component:
@@ -248,6 +256,12 @@ Similarly, this example relies on manual user clicks on a "Fetch More" button to
248256

249257
The endpoint itself only defines `getNextPageParam`, so this example doesn't support fetching backwards, but that can be provided in cases where backwards fetching makes sense. The page param here is a simple incremented number, but the page param
250258

259+
## Refetching
260+
261+
When an infinite query endpoint is refetched (due to tag invalidation, polling, arg change configuration, or manual refetching), RTK Query will try to sequentially refetch all pages currently in the cache. This ensures that the client is always working with the latest data, and avoids stale cursors or duplicate records.
262+
263+
If the cache entry is ever removed and then re-added, it will start with only fetching the initial page.
264+
251265
## Limiting Cache Entry Size
252266

253267
All fetched pages for a given query arg are stored in the `pages` array in that cache entry. By default, there is no limit to the number of stored pages - if you call `fetchNextPage()` 1000 times, `data.pages` will have 1000 pages stored.
@@ -260,6 +274,8 @@ The `getNext/PreviousPageParam` callbacks offer flexibility in how you interact
260274

261275
Here are some examples of common infinite query patterns to show how you might approach different use cases.
262276

277+
For additional examples, and to see some of these patterns in action, see [the RTK Query "infinite queries" example app in the repo](https://github.com/reduxjs/redux-toolkit/tree/master/examples/query/react/infinite-queries).
278+
263279
### Basic Pagination
264280

265281
For a simple API that just needs page numbers, you can calculate the previous and next page numbers based on the existing page params:
Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
1-
# vite-template-redux
1+
# RTK Query Infinite Queries Example
22

3-
Uses [Vite](https://vitejs.dev/), [Vitest](https://vitest.dev/), and [React Testing Library](https://github.com/testing-library/react-testing-library) to create a modern [React](https://react.dev/) app compatible with [Create React App](https://create-react-app.dev/)
3+
This example shows a variety of usage patterns for RTK Query's infinite query endpoint support, including:
44

5-
```sh
6-
npx degit reduxjs/redux-templates/packages/vite-template-redux my-app
7-
```
8-
9-
## Goals
10-
11-
- Easy migration from Create React App or Vite
12-
- As beginner friendly as Create React App
13-
- Optimized performance compared to Create React App
14-
- Customizable without ejecting
15-
16-
## Scripts
17-
18-
- `dev`/`start` - start dev server and open browser
19-
- `build` - build for production
20-
- `preview` - locally preview production build
21-
- `test` - launch test runner
22-
23-
## Inspiration
24-
25-
- [Create React App](https://github.com/facebook/create-react-app/tree/main/packages/cra-template)
26-
- [Vite](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react)
27-
- [Vitest](https://github.com/vitest-dev/vitest/tree/main/examples/react-testing-lib)
5+
- Basic pagination
6+
- Infinite scrolling
7+
- Bidirectional cursors
8+
- Offset + limit
9+
- Max pages
10+
- React Native FlatList

0 commit comments

Comments
 (0)