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

useInfiniteQuery.ts

263 lines9.1 KB

1import { InfiniteQueryObserver } from '@tanstack/query-core'
2import { createMemo } from 'solid-js'
3import { useBaseQuery } from './useBaseQuery'
4import type {
5 DefaultError,
6 InfiniteData,
7 InfiniteQueryMode,
8 QueryKey,
9 QueryObserver,
10} from '@tanstack/query-core'
11import type { QueryClient } from './QueryClient'
12import type {
13 DefinedUseInfiniteQueryResult,
14 UseInfiniteQueryOptions,
15 UseInfiniteQueryResult,
16} from './types'
17import type { Accessor } from 'solid-js'
18import type {
19 DefinedInitialDataInfiniteOptions,
20 UndefinedInitialDataInfiniteOptions,
21} from './infiniteQueryOptions'
22​
23/**
24 * The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
25 * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
26 *
27 * This overload is selected when `initialData` is set.
28 * @param options - An accessor returning the {@link DefinedInitialDataInfiniteOptions} to use β€” everything you
29 * can pass to `useInfiniteQuery`, with `initialData` set.
30 * @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
31 * will be used.
32 * @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
33 * `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
34 * `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
35 * `InfiniteData<TQueryFnData>` shape.
36 * @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
37 * refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
38 * actions, or add conditions like `hasNextPage && !isFetching`.
39 * @see {@link infiniteQueryOptions} to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.
40 * @example
41 * ```tsx
42 * import { For } from 'solid-js'
43 * import { useInfiniteQuery } from '@tanstack/solid-query'
44 *
45 * function Projects() {
46 * // `projectsQuery.data` is never `undefined`, thanks to `initialData` β€” even if a refetch fails, so the
47 * // list stays visible alongside the error.
48 * const projectsQuery = useInfiniteQuery(() => ({
49 * queryKey: ['projects'],
50 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
51 * initialPageParam: 0,
52 * getNextPageParam: (lastPage) => lastPage.nextId,
53 * initialData: { pages: [], pageParams: [] },
54 * }))
55 *
56 * return (
57 * <div>
58 * {projectsQuery.isError ? <span>Error: {projectsQuery.error.message}</span> : null}
59 * <ul>
60 * <For each={projectsQuery.data.pages}>
61 * {(page) => <For each={page.projects}>{(p) => <li>{p.name}</li>}</For>}
62 * </For>
63 * </ul>
64 * </div>
65 * )
66 * }
67 * ```
68 */
69export function useInfiniteQuery<
70 TQueryFnData,
71 TError = DefaultError,
72 TData = InfiniteData<TQueryFnData>,
73 TQueryKey extends QueryKey = QueryKey,
74 TPageParam = unknown,
75>(
76 options: DefinedInitialDataInfiniteOptions<
77 TQueryFnData,
78 TError,
79 TData,
80 TQueryKey,
81 TPageParam,
82 undefined
83 >,
84 queryClient?: Accessor<QueryClient>,
85): DefinedUseInfiniteQueryResult<TData, TError, TPageParam, undefined>
86export function useInfiniteQuery<
87 TQueryFnData,
88 TError = DefaultError,
89 TData = InfiniteData<TQueryFnData>,
90 TQueryKey extends QueryKey = QueryKey,
91 TPageParam = unknown,
92>(
93 options: DefinedInitialDataInfiniteOptions<
94 TQueryFnData,
95 TError,
96 TData,
97 TQueryKey,
98 TPageParam,
99 InfiniteQueryMode
100 >,
101 queryClient?: Accessor<QueryClient>,
102): DefinedUseInfiniteQueryResult<TData, TError, TPageParam, InfiniteQueryMode>
103​
104/**
105 * The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
106 * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
107 * @param options - An accessor returning the {@link UndefinedInitialDataInfiniteOptions} to use β€” everything
108 * you can pass to `useInfiniteQuery`.
109 * @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
110 * will be used.
111 * @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
112 * `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
113 * `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
114 * `InfiniteData<TQueryFnData>` shape.
115 * @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
116 * refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
117 * actions, or add conditions like `hasNextPage && !isFetching`.
118 * @see {@link infiniteQueryOptions} to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.
119 * @example
120 * Fetching the next page from a "Load More" button click:
121 * ```tsx
122 * import { For, Match, Switch } from 'solid-js'
123 * import { useInfiniteQuery } from '@tanstack/solid-query'
124 *
125 * function Projects() {
126 * const projectsQuery = useInfiniteQuery(() => ({
127 * queryKey: ['projects'],
128 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
129 * initialPageParam: 0,
130 * getNextPageParam: (lastPage) => lastPage.nextId,
131 * }))
132 *
133 * return (
134 * <Switch>
135 * <Match when={projectsQuery.isPending}>Loading...</Match>
136 * <Match when={projectsQuery.isError}>Error: {projectsQuery.error.message}</Match>
137 * <Match when={projectsQuery.isSuccess}>
138 * <ul>
139 * <For each={projectsQuery.data.pages}>
140 * {(page) => <For each={page.projects}>{(p) => <li>{p.name}</li>}</For>}
141 * </For>
142 * </ul>
143 * <button
144 * onClick={() => projectsQuery.fetchNextPage()}
145 * disabled={!projectsQuery.hasNextPage || projectsQuery.isFetching}
146 * >
147 * {projectsQuery.isFetchingNextPage
148 * ? 'Loading more...'
149 * : projectsQuery.hasNextPage
150 * ? 'Load More'
151 * : 'Nothing more to load'}
152 * </button>
153 * </Match>
154 * </Switch>
155 * )
156 * }
157 * ```
158 * @example
159 * Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
160 * sentinel element after the list:
161 * ```tsx
162 * import { For, Match, Switch, createEffect, onCleanup } from 'solid-js'
163 * import { useInfiniteQuery } from '@tanstack/solid-query'
164 *
165 * function Projects() {
166 * const projectsQuery = useInfiniteQuery(() => ({
167 * queryKey: ['projects'],
168 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
169 * initialPageParam: 0,
170 * getNextPageParam: (lastPage) => lastPage.nextId,
171 * }))
172 *
173 * let sentinelRef: HTMLDivElement | undefined
174 *
175 * createEffect(() => {
176 * if (sentinelRef == null || !projectsQuery.hasNextPage || projectsQuery.isFetching) return
177 *
178 * const observer = new IntersectionObserver(([entry]) => {
179 * if (entry?.isIntersecting) projectsQuery.fetchNextPage()
180 * })
181 * observer.observe(sentinelRef)
182 *
183 * onCleanup(() => observer.disconnect())
184 * })
185 *
186 * return (
187 * <Switch>
188 * <Match when={projectsQuery.isPending}>Loading...</Match>
189 * <Match when={projectsQuery.isError}>Error: {projectsQuery.error.message}</Match>
190 * <Match when={projectsQuery.isSuccess}>
191 * <ul>
192 * <For each={projectsQuery.data.pages}>
193 * {(page) => <For each={page.projects}>{(p) => <li>{p.name}</li>}</For>}
194 * </For>
195 * </ul>
196 * <div ref={sentinelRef}>{projectsQuery.isFetchingNextPage ? 'Loading more...' : null}</div>
197 * </Match>
198 * </Switch>
199 * )
200 * }
201 * ```
202 */
203export function useInfiniteQuery<
204 TQueryFnData,
205 TError = DefaultError,
206 TData = InfiniteData<TQueryFnData>,
207 TQueryKey extends QueryKey = QueryKey,
208 TPageParam = unknown,
209>(
210 options: UndefinedInitialDataInfiniteOptions<
211 TQueryFnData,
212 TError,
213 TData,
214 TQueryKey,
215 TPageParam,
216 undefined
217 >,
218 queryClient?: Accessor<QueryClient>,
219): UseInfiniteQueryResult<TData, TError, TPageParam, undefined>
220export function useInfiniteQuery<
221 TQueryFnData,
222 TError = DefaultError,
223 TData = InfiniteData<TQueryFnData>,
224 TQueryKey extends QueryKey = QueryKey,
225 TPageParam = unknown,
226>(
227 options: UndefinedInitialDataInfiniteOptions<
228 TQueryFnData,
229 TError,
230 TData,
231 TQueryKey,
232 TPageParam,
233 InfiniteQueryMode
234 >,
235 queryClient?: Accessor<QueryClient>,
236): UseInfiniteQueryResult<TData, TError, TPageParam, InfiniteQueryMode>
237export function useInfiniteQuery<
238 TQueryFnData,
239 TError = DefaultError,
240 TData = InfiniteData<TQueryFnData>,
241 TQueryKey extends QueryKey = QueryKey,
242 TPageParam = unknown,
243>(
244 options: UseInfiniteQueryOptions<
245 TQueryFnData,
246 TError,
247 TData,
248 TQueryKey,
249 TPageParam
250 >,
251 queryClient?: Accessor<QueryClient>,
252): UseInfiniteQueryResult<TData, TError, TPageParam>
253​
254export function useInfiniteQuery(
255 options: any,
256 queryClient?: Accessor<QueryClient>,
257): any {
258 return useBaseQuery(
259 createMemo(() => options()),
260 InfiniteQueryObserver as typeof QueryObserver,
261 queryClient,
262 ) as UseInfiniteQueryResult