Skip to content

Commit 701a1b4

Browse files
committed
feat(infiniteQuery): add possibility to decide which pages should be refetched
documentation for refetchPage
1 parent a4ff941 commit 701a1b4

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

docs/src/pages/guides/infinite-queries.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@ function Projects() {
9595

9696
When an infinite query becomes `stale` and needs to be refetched, each group is fetched `sequentially`, starting from the first one. This ensures that even if the underlying data is mutated, we're not using stale cursors and potentially getting duplicates or skipping records. If an infinite query's results are ever removed from the queryCache, the pagination restarts at the initial state with only the initial group being requested.
9797

98+
### refetchPage
99+
100+
If you only want to actively refetch a subset of all pages, you can pass the `refetchPage` function to `refetch` returned from `useInfiniteQuery`.
101+
102+
```js
103+
const { refetch } = useInfiniteQuery('projects', fetchProjects, {
104+
getNextPageParam: (lastPage, pages) => lastPage.nextCursor,
105+
})
106+
107+
// only refetch the first page
108+
refetch({ refetchPage: (page, index) => index === 0 })
109+
```
110+
111+
You can also pass this function as part of the 3rd argument (`options`) to [queryClient.refetchQueries](/reference/QueryClient#queryclientrefetchqueries), [queryClient.invalidateQueries](/reference/QueryClient#queryclientinvalidatequeries) or [queryClient.resetQueries](/reference/QueryClient#queryclientresetqueries).
112+
113+
**Signature**
114+
115+
- `refetchPage: (page: TData, index: number, allPages: TData[]) => boolean`
116+
117+
The function is executed for each page, and only pages where this function returns `true` will be refetched.
118+
98119
## What if I need to pass custom information to my query function?
99120

100121
By default, the variable returned from `getNextPageParam` will be supplied to the query function, but in some cases, you may want to override this. You can pass custom variables to the `fetchNextPage` function which will override the default variable like so:

docs/src/pages/reference/QueryClient.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ const data = queryClient.getQueriesData(queryKey | filters)
194194

195195
- `[queryKey:QueryKey, data:TData | unknown][]`
196196
- An array of tuples for the matched query keys, or `[]` if there are no matches. The tuples are the query key and its associated data.
197-
197+
198198
**Caveats**
199199

200200
Because the returned data in each tuple can be of varying structures (i.e. using a filter to return "active" queries can return different data types), the `TData` generic defaults to `unknown`. If you provide a more specific type to `TData` it is assumed that you are certain each tuple's data entry is all the same type.
@@ -290,6 +290,9 @@ await queryClient.invalidateQueries('posts', {
290290
- `refetchOptions?: RefetchOptions`:
291291
- `throwOnError?: boolean`
292292
- When set to `true`, this method will throw if any of the query refetch tasks fail.
293+
- `refetchPage: (page: TData, index: number, allPages: TData[]) => boolean`
294+
- Only for [Infinite Queries](../guides/infinite-queries#refetchpage)
295+
- Use this function to specify which pages should be refetched
293296

294297
## `queryClient.refetchQueries`
295298

@@ -318,6 +321,9 @@ await queryClient.refetchQueries(['posts', 1], { active: true, exact: true })
318321
- `refetchOptions?: RefetchOptions`:
319322
- `throwOnError?: boolean`
320323
- When set to `true`, this method will throw if any of the query refetch tasks fail.
324+
- `refetchPage: (page: TData, index: number, allPages: TData[]) => boolean`
325+
- Only for [Infinite Queries](../guides/infinite-queries#refetchpage)
326+
- Use this function to specify which pages should be refetched
321327

322328
**Returns**
323329

@@ -381,6 +387,9 @@ queryClient.resetQueries(queryKey, { exact: true })
381387
- `resetOptions?: ResetOptions`:
382388
- `throwOnError?: boolean`
383389
- When set to `true`, this method will throw if any of the query refetch tasks fail.
390+
- `refetchPage: (page: TData, index: number, allPages: TData[]) => boolean`
391+
- Only for [Infinite Queries](../guides/infinite-queries#refetchpage)
392+
- Use this function to specify which pages should be refetched
384393

385394
**Returns**
386395

0 commit comments

Comments
 (0)