@tanstack/preact-query 5.102.8 → 5.103.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/HydrationBoundary.cjs +1 -1
- package/build/legacy/HydrationBoundary.cjs.map +1 -1
- package/build/legacy/HydrationBoundary.js +1 -1
- package/build/legacy/HydrationBoundary.js.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.cjs +12 -12
- package/build/legacy/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.d.cts +12 -12
- package/build/legacy/QueryErrorResetBoundary.d.ts +12 -12
- package/build/legacy/QueryErrorResetBoundary.js +12 -12
- package/build/legacy/QueryErrorResetBoundary.js.map +1 -1
- package/build/legacy/infiniteQueryOptions.cjs.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.cts +41 -55
- package/build/legacy/infiniteQueryOptions.d.cts.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.ts +41 -55
- package/build/legacy/infiniteQueryOptions.d.ts.map +1 -1
- package/build/legacy/infiniteQueryOptions.js.map +1 -1
- package/build/legacy/mutationOptions.cjs.map +1 -1
- package/build/legacy/mutationOptions.d.cts +4 -17
- package/build/legacy/mutationOptions.d.cts.map +1 -1
- package/build/legacy/mutationOptions.d.ts +4 -17
- package/build/legacy/mutationOptions.d.ts.map +1 -1
- package/build/legacy/mutationOptions.js.map +1 -1
- package/build/legacy/queryOptions.cjs.map +1 -1
- package/build/legacy/queryOptions.d.cts +51 -74
- package/build/legacy/queryOptions.d.cts.map +1 -1
- package/build/legacy/queryOptions.d.ts +51 -74
- package/build/legacy/queryOptions.d.ts.map +1 -1
- package/build/legacy/queryOptions.js.map +1 -1
- package/build/legacy/useInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useInfiniteQuery.d.cts +208 -36
- package/build/legacy/useInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useInfiniteQuery.d.ts +208 -36
- package/build/legacy/useInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useInfiniteQuery.js.map +1 -1
- package/build/legacy/useIsFetching.cjs +8 -6
- package/build/legacy/useIsFetching.cjs.map +1 -1
- package/build/legacy/useIsFetching.d.cts +8 -6
- package/build/legacy/useIsFetching.d.cts.map +1 -1
- package/build/legacy/useIsFetching.d.ts +8 -6
- package/build/legacy/useIsFetching.d.ts.map +1 -1
- package/build/legacy/useIsFetching.js +8 -6
- package/build/legacy/useIsFetching.js.map +1 -1
- package/build/legacy/useMutation.cjs +13 -11
- package/build/legacy/useMutation.cjs.map +1 -1
- package/build/legacy/useMutation.d.cts +13 -11
- package/build/legacy/useMutation.d.cts.map +1 -1
- package/build/legacy/useMutation.d.ts +13 -11
- package/build/legacy/useMutation.d.ts.map +1 -1
- package/build/legacy/useMutation.js +13 -11
- package/build/legacy/useMutation.js.map +1 -1
- package/build/legacy/useMutationState.cjs +49 -29
- package/build/legacy/useMutationState.cjs.map +1 -1
- package/build/legacy/useMutationState.d.cts +49 -29
- package/build/legacy/useMutationState.d.cts.map +1 -1
- package/build/legacy/useMutationState.d.ts +49 -29
- package/build/legacy/useMutationState.d.ts.map +1 -1
- package/build/legacy/useMutationState.js +49 -29
- package/build/legacy/useMutationState.js.map +1 -1
- package/build/legacy/useQueries.cjs +50 -23
- package/build/legacy/useQueries.cjs.map +1 -1
- package/build/legacy/useQueries.d.cts +50 -23
- package/build/legacy/useQueries.d.cts.map +1 -1
- package/build/legacy/useQueries.d.ts +50 -23
- package/build/legacy/useQueries.d.ts.map +1 -1
- package/build/legacy/useQueries.js +50 -23
- package/build/legacy/useQueries.js.map +1 -1
- package/build/legacy/useQuery.cjs.map +1 -1
- package/build/legacy/useQuery.d.cts +93 -33
- package/build/legacy/useQuery.d.cts.map +1 -1
- package/build/legacy/useQuery.d.ts +93 -33
- package/build/legacy/useQuery.d.ts.map +1 -1
- package/build/legacy/useQuery.js.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.cjs +57 -7
- package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.cts +57 -7
- package/build/legacy/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.ts +57 -7
- package/build/legacy/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.js +57 -7
- package/build/legacy/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/legacy/useSuspenseQueries.cjs.map +1 -1
- package/build/legacy/useSuspenseQueries.d.cts +244 -20
- package/build/legacy/useSuspenseQueries.d.cts.map +1 -1
- package/build/legacy/useSuspenseQueries.d.ts +244 -20
- package/build/legacy/useSuspenseQueries.d.ts.map +1 -1
- package/build/legacy/useSuspenseQueries.js.map +1 -1
- package/build/legacy/useSuspenseQuery.cjs +48 -8
- package/build/legacy/useSuspenseQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseQuery.d.cts +48 -8
- package/build/legacy/useSuspenseQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseQuery.d.ts +48 -8
- package/build/legacy/useSuspenseQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseQuery.js +48 -8
- package/build/legacy/useSuspenseQuery.js.map +1 -1
- package/build/modern/HydrationBoundary.cjs +1 -1
- package/build/modern/HydrationBoundary.cjs.map +1 -1
- package/build/modern/HydrationBoundary.js +1 -1
- package/build/modern/HydrationBoundary.js.map +1 -1
- package/build/modern/QueryErrorResetBoundary.cjs +12 -12
- package/build/modern/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/modern/QueryErrorResetBoundary.d.cts +12 -12
- package/build/modern/QueryErrorResetBoundary.d.ts +12 -12
- package/build/modern/QueryErrorResetBoundary.js +12 -12
- package/build/modern/QueryErrorResetBoundary.js.map +1 -1
- package/build/modern/infiniteQueryOptions.cjs.map +1 -1
- package/build/modern/infiniteQueryOptions.d.cts +41 -55
- package/build/modern/infiniteQueryOptions.d.cts.map +1 -1
- package/build/modern/infiniteQueryOptions.d.ts +41 -55
- package/build/modern/infiniteQueryOptions.d.ts.map +1 -1
- package/build/modern/infiniteQueryOptions.js.map +1 -1
- package/build/modern/mutationOptions.cjs.map +1 -1
- package/build/modern/mutationOptions.d.cts +4 -17
- package/build/modern/mutationOptions.d.cts.map +1 -1
- package/build/modern/mutationOptions.d.ts +4 -17
- package/build/modern/mutationOptions.d.ts.map +1 -1
- package/build/modern/mutationOptions.js.map +1 -1
- package/build/modern/queryOptions.cjs.map +1 -1
- package/build/modern/queryOptions.d.cts +51 -74
- package/build/modern/queryOptions.d.cts.map +1 -1
- package/build/modern/queryOptions.d.ts +51 -74
- package/build/modern/queryOptions.d.ts.map +1 -1
- package/build/modern/queryOptions.js.map +1 -1
- package/build/modern/useInfiniteQuery.cjs.map +1 -1
- package/build/modern/useInfiniteQuery.d.cts +208 -36
- package/build/modern/useInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useInfiniteQuery.d.ts +208 -36
- package/build/modern/useInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useInfiniteQuery.js.map +1 -1
- package/build/modern/useIsFetching.cjs +8 -6
- package/build/modern/useIsFetching.cjs.map +1 -1
- package/build/modern/useIsFetching.d.cts +8 -6
- package/build/modern/useIsFetching.d.cts.map +1 -1
- package/build/modern/useIsFetching.d.ts +8 -6
- package/build/modern/useIsFetching.d.ts.map +1 -1
- package/build/modern/useIsFetching.js +8 -6
- package/build/modern/useIsFetching.js.map +1 -1
- package/build/modern/useMutation.cjs +13 -11
- package/build/modern/useMutation.cjs.map +1 -1
- package/build/modern/useMutation.d.cts +13 -11
- package/build/modern/useMutation.d.cts.map +1 -1
- package/build/modern/useMutation.d.ts +13 -11
- package/build/modern/useMutation.d.ts.map +1 -1
- package/build/modern/useMutation.js +13 -11
- package/build/modern/useMutation.js.map +1 -1
- package/build/modern/useMutationState.cjs +49 -29
- package/build/modern/useMutationState.cjs.map +1 -1
- package/build/modern/useMutationState.d.cts +49 -29
- package/build/modern/useMutationState.d.cts.map +1 -1
- package/build/modern/useMutationState.d.ts +49 -29
- package/build/modern/useMutationState.d.ts.map +1 -1
- package/build/modern/useMutationState.js +49 -29
- package/build/modern/useMutationState.js.map +1 -1
- package/build/modern/useQueries.cjs +50 -23
- package/build/modern/useQueries.cjs.map +1 -1
- package/build/modern/useQueries.d.cts +50 -23
- package/build/modern/useQueries.d.cts.map +1 -1
- package/build/modern/useQueries.d.ts +50 -23
- package/build/modern/useQueries.d.ts.map +1 -1
- package/build/modern/useQueries.js +50 -23
- package/build/modern/useQueries.js.map +1 -1
- package/build/modern/useQuery.cjs.map +1 -1
- package/build/modern/useQuery.d.cts +93 -33
- package/build/modern/useQuery.d.cts.map +1 -1
- package/build/modern/useQuery.d.ts +93 -33
- package/build/modern/useQuery.d.ts.map +1 -1
- package/build/modern/useQuery.js.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.cjs +57 -7
- package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.cts +57 -7
- package/build/modern/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.ts +57 -7
- package/build/modern/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.js +57 -7
- package/build/modern/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/modern/useSuspenseQueries.cjs.map +1 -1
- package/build/modern/useSuspenseQueries.d.cts +244 -20
- package/build/modern/useSuspenseQueries.d.cts.map +1 -1
- package/build/modern/useSuspenseQueries.d.ts +244 -20
- package/build/modern/useSuspenseQueries.d.ts.map +1 -1
- package/build/modern/useSuspenseQueries.js.map +1 -1
- package/build/modern/useSuspenseQuery.cjs +48 -8
- package/build/modern/useSuspenseQuery.cjs.map +1 -1
- package/build/modern/useSuspenseQuery.d.cts +48 -8
- package/build/modern/useSuspenseQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseQuery.d.ts +48 -8
- package/build/modern/useSuspenseQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseQuery.js +48 -8
- package/build/modern/useSuspenseQuery.js.map +1 -1
- package/build/query-codemods/eslint.config.js +7 -0
- package/build/query-codemods/root.eslint.config.js +45 -0
- package/package.json +11 -11
- package/src/HydrationBoundary.tsx +0 -1
- package/src/QueryErrorResetBoundary.tsx +12 -12
- package/src/infiniteQueryOptions.ts +41 -55
- package/src/mutationOptions.ts +4 -17
- package/src/queryOptions.ts +50 -73
- package/src/useInfiniteQuery.ts +208 -36
- package/src/useIsFetching.ts +8 -6
- package/src/useMutation.ts +13 -11
- package/src/useMutationState.ts +49 -29
- package/src/useQueries.ts +50 -23
- package/src/useQuery.ts +93 -33
- package/src/useSuspenseInfiniteQuery.ts +57 -7
- package/src/useSuspenseQueries.ts +244 -20
- package/src/useSuspenseQuery.ts +48 -8
|
@@ -3,7 +3,7 @@ import { DefinedInitialDataInfiniteOptions, UndefinedInitialDataInfiniteOptions
|
|
|
3
3
|
import { DefaultError, InfiniteData, QueryClient, QueryKey } from "@tanstack/query-core";
|
|
4
4
|
//#region src/useInfiniteQuery.d.ts
|
|
5
5
|
/**
|
|
6
|
-
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
6
|
+
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
7
7
|
* `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
|
|
8
8
|
*
|
|
9
9
|
* This overload is selected when `initialData` is set.
|
|
@@ -15,16 +15,19 @@ import { DefaultError, InfiniteData, QueryClient, QueryKey } from "@tanstack/que
|
|
|
15
15
|
* @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`, with `initialData` set.
|
|
16
16
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
17
17
|
* be used.
|
|
18
|
-
* @returns The same properties as `useQuery`, with the addition of `
|
|
19
|
-
* `
|
|
20
|
-
* `
|
|
18
|
+
* @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
|
|
19
|
+
* `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
|
|
20
|
+
* `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
|
|
21
|
+
* `InfiniteData<TQueryFnData>` shape.
|
|
21
22
|
*
|
|
22
23
|
* @example
|
|
23
24
|
* ```tsx
|
|
24
25
|
* import { useInfiniteQuery } from '@tanstack/preact-query'
|
|
25
26
|
*
|
|
26
27
|
* function Projects() {
|
|
27
|
-
*
|
|
28
|
+
* // `data` is never `undefined`, thanks to `initialData` — even if a refetch fails, so the
|
|
29
|
+
* // list stays visible alongside the error.
|
|
30
|
+
* const { data, isError, error } = useInfiniteQuery({
|
|
28
31
|
* queryKey: ['projects'],
|
|
29
32
|
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
30
33
|
* initialPageParam: 0,
|
|
@@ -32,13 +35,20 @@ import { DefaultError, InfiniteData, QueryClient, QueryKey } from "@tanstack/que
|
|
|
32
35
|
* initialData: { pages: [], pageParams: [] },
|
|
33
36
|
* })
|
|
34
37
|
*
|
|
35
|
-
* return
|
|
38
|
+
* return (
|
|
39
|
+
* <div>
|
|
40
|
+
* {isError ? <span>Error: {error.message}</span> : null}
|
|
41
|
+
* <ul>
|
|
42
|
+
* {data.pages.map((page) => page.projects.map((p) => <li key={p.id}>{p.name}</li>))}
|
|
43
|
+
* </ul>
|
|
44
|
+
* </div>
|
|
45
|
+
* )
|
|
36
46
|
* }
|
|
37
47
|
* ```
|
|
38
48
|
*/
|
|
39
49
|
declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(options: DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: QueryClient): DefinedUseInfiniteQueryResult<TData, TError>;
|
|
40
50
|
/**
|
|
41
|
-
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
51
|
+
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
42
52
|
* `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
|
|
43
53
|
*
|
|
44
54
|
* @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
|
|
@@ -48,16 +58,18 @@ declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = I
|
|
|
48
58
|
* @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`.
|
|
49
59
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
50
60
|
* be used.
|
|
51
|
-
* @returns The same properties as `useQuery`, with the addition of `
|
|
52
|
-
* `
|
|
53
|
-
* `
|
|
61
|
+
* @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
|
|
62
|
+
* `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
|
|
63
|
+
* `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
|
|
64
|
+
* `InfiniteData<TQueryFnData>` shape.
|
|
54
65
|
*
|
|
55
66
|
* @example
|
|
67
|
+
* Fetching the next page from a "Load More" button click:
|
|
56
68
|
* ```tsx
|
|
57
69
|
* import { useInfiniteQuery } from '@tanstack/preact-query'
|
|
58
70
|
*
|
|
59
71
|
* function Projects() {
|
|
60
|
-
* const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
|
|
72
|
+
* const { data, isPending, isError, error, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
|
|
61
73
|
* useInfiniteQuery({
|
|
62
74
|
* queryKey: ['projects'],
|
|
63
75
|
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
@@ -65,24 +77,88 @@ declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = I
|
|
|
65
77
|
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
66
78
|
* })
|
|
67
79
|
*
|
|
80
|
+
* if (isPending) return 'Loading...'
|
|
81
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
82
|
+
*
|
|
68
83
|
* return (
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
84
|
+
* <>
|
|
85
|
+
* <ul>
|
|
86
|
+
* {data.pages.map((page) =>
|
|
87
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
88
|
+
* )}
|
|
89
|
+
* </ul>
|
|
90
|
+
* <button
|
|
91
|
+
* onClick={() => fetchNextPage()}
|
|
92
|
+
* disabled={!hasNextPage || isFetching}
|
|
93
|
+
* >
|
|
94
|
+
* {isFetchingNextPage
|
|
95
|
+
* ? 'Loading more...'
|
|
96
|
+
* : hasNextPage
|
|
97
|
+
* ? 'Load More'
|
|
98
|
+
* : 'Nothing more to load'}
|
|
99
|
+
* </button>
|
|
100
|
+
* </>
|
|
101
|
+
* )
|
|
102
|
+
* }
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
|
|
107
|
+
* sentinel element after the list:
|
|
108
|
+
* ```tsx
|
|
109
|
+
* import { useInfiniteQuery } from '@tanstack/preact-query'
|
|
110
|
+
* import { useEffect, useRef } from 'preact/hooks'
|
|
111
|
+
*
|
|
112
|
+
* function Projects() {
|
|
113
|
+
* const {
|
|
114
|
+
* data,
|
|
115
|
+
* isPending,
|
|
116
|
+
* isError,
|
|
117
|
+
* error,
|
|
118
|
+
* fetchNextPage,
|
|
119
|
+
* hasNextPage,
|
|
120
|
+
* isFetching,
|
|
121
|
+
* isFetchingNextPage,
|
|
122
|
+
* } = useInfiniteQuery({
|
|
123
|
+
* queryKey: ['projects'],
|
|
124
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
125
|
+
* initialPageParam: 0,
|
|
126
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
127
|
+
* })
|
|
128
|
+
*
|
|
129
|
+
* const sentinelRef = useRef<HTMLDivElement>(null)
|
|
130
|
+
*
|
|
131
|
+
* useEffect(() => {
|
|
132
|
+
* const sentinel = sentinelRef.current
|
|
133
|
+
* if (sentinel == null || !hasNextPage || isFetching) return
|
|
134
|
+
*
|
|
135
|
+
* const observer = new IntersectionObserver(([entry]) => {
|
|
136
|
+
* if (entry?.isIntersecting) fetchNextPage()
|
|
137
|
+
* })
|
|
138
|
+
* observer.observe(sentinel)
|
|
139
|
+
*
|
|
140
|
+
* return () => observer.disconnect()
|
|
141
|
+
* }, [hasNextPage, isFetching, fetchNextPage])
|
|
142
|
+
*
|
|
143
|
+
* if (isPending) return 'Loading...'
|
|
144
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
145
|
+
*
|
|
146
|
+
* return (
|
|
147
|
+
* <>
|
|
148
|
+
* <ul>
|
|
149
|
+
* {data.pages.map((page) =>
|
|
150
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
151
|
+
* )}
|
|
152
|
+
* </ul>
|
|
153
|
+
* <div ref={sentinelRef}>{isFetchingNextPage ? 'Loading more...' : null}</div>
|
|
154
|
+
* </>
|
|
79
155
|
* )
|
|
80
156
|
* }
|
|
81
157
|
* ```
|
|
82
158
|
*/
|
|
83
159
|
declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(options: UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: QueryClient): UseInfiniteQueryResult<TData, TError>;
|
|
84
160
|
/**
|
|
85
|
-
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
161
|
+
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
86
162
|
* `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
|
|
87
163
|
*
|
|
88
164
|
* @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
|
|
@@ -92,16 +168,18 @@ declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = I
|
|
|
92
168
|
* @param options - The {@link UseInfiniteQueryOptions} to use — everything you can pass to `useInfiniteQuery`.
|
|
93
169
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
94
170
|
* be used.
|
|
95
|
-
* @returns The same properties as `useQuery`, with the addition of `
|
|
96
|
-
* `
|
|
97
|
-
* `
|
|
171
|
+
* @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
|
|
172
|
+
* `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
|
|
173
|
+
* `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
|
|
174
|
+
* `InfiniteData<TQueryFnData>` shape.
|
|
98
175
|
*
|
|
99
176
|
* @example
|
|
177
|
+
* Fetching the next page from a "Load More" button click:
|
|
100
178
|
* ```tsx
|
|
101
179
|
* import { useInfiniteQuery } from '@tanstack/preact-query'
|
|
102
180
|
*
|
|
103
181
|
* function Projects() {
|
|
104
|
-
* const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
|
|
182
|
+
* const { data, isPending, isError, error, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
|
|
105
183
|
* useInfiniteQuery({
|
|
106
184
|
* queryKey: ['projects'],
|
|
107
185
|
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
@@ -109,17 +187,111 @@ declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = I
|
|
|
109
187
|
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
110
188
|
* })
|
|
111
189
|
*
|
|
190
|
+
* if (isPending) return 'Loading...'
|
|
191
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
192
|
+
*
|
|
193
|
+
* return (
|
|
194
|
+
* <>
|
|
195
|
+
* <ul>
|
|
196
|
+
* {data.pages.map((page) =>
|
|
197
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
198
|
+
* )}
|
|
199
|
+
* </ul>
|
|
200
|
+
* <button
|
|
201
|
+
* onClick={() => fetchNextPage()}
|
|
202
|
+
* disabled={!hasNextPage || isFetching}
|
|
203
|
+
* >
|
|
204
|
+
* {isFetchingNextPage
|
|
205
|
+
* ? 'Loading more...'
|
|
206
|
+
* : hasNextPage
|
|
207
|
+
* ? 'Load More'
|
|
208
|
+
* : 'Nothing more to load'}
|
|
209
|
+
* </button>
|
|
210
|
+
* </>
|
|
211
|
+
* )
|
|
212
|
+
* }
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
|
|
217
|
+
* sentinel element after the list:
|
|
218
|
+
* ```tsx
|
|
219
|
+
* import { useInfiniteQuery } from '@tanstack/preact-query'
|
|
220
|
+
* import { useEffect, useRef } from 'preact/hooks'
|
|
221
|
+
*
|
|
222
|
+
* function Projects() {
|
|
223
|
+
* const {
|
|
224
|
+
* data,
|
|
225
|
+
* isPending,
|
|
226
|
+
* isError,
|
|
227
|
+
* error,
|
|
228
|
+
* fetchNextPage,
|
|
229
|
+
* hasNextPage,
|
|
230
|
+
* isFetching,
|
|
231
|
+
* isFetchingNextPage,
|
|
232
|
+
* } = useInfiniteQuery({
|
|
233
|
+
* queryKey: ['projects'],
|
|
234
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
235
|
+
* initialPageParam: 0,
|
|
236
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
237
|
+
* })
|
|
238
|
+
*
|
|
239
|
+
* const sentinelRef = useRef<HTMLDivElement>(null)
|
|
240
|
+
*
|
|
241
|
+
* useEffect(() => {
|
|
242
|
+
* const sentinel = sentinelRef.current
|
|
243
|
+
* if (sentinel == null || !hasNextPage || isFetching) return
|
|
244
|
+
*
|
|
245
|
+
* const observer = new IntersectionObserver(([entry]) => {
|
|
246
|
+
* if (entry?.isIntersecting) fetchNextPage()
|
|
247
|
+
* })
|
|
248
|
+
* observer.observe(sentinel)
|
|
249
|
+
*
|
|
250
|
+
* return () => observer.disconnect()
|
|
251
|
+
* }, [hasNextPage, isFetching, fetchNextPage])
|
|
252
|
+
*
|
|
253
|
+
* if (isPending) return 'Loading...'
|
|
254
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
255
|
+
*
|
|
256
|
+
* return (
|
|
257
|
+
* <>
|
|
258
|
+
* <ul>
|
|
259
|
+
* {data.pages.map((page) =>
|
|
260
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
261
|
+
* )}
|
|
262
|
+
* </ul>
|
|
263
|
+
* <div ref={sentinelRef}>{isFetchingNextPage ? 'Loading more...' : null}</div>
|
|
264
|
+
* </>
|
|
265
|
+
* )
|
|
266
|
+
* }
|
|
267
|
+
* ```
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* A query that's disabled, type safe, until `postId` is set — pass `skipToken` as `queryFn`
|
|
271
|
+
* instead of setting `enabled: false`:
|
|
272
|
+
* ```tsx
|
|
273
|
+
* import { skipToken, useInfiniteQuery } from '@tanstack/preact-query'
|
|
274
|
+
*
|
|
275
|
+
* function Comments({ postId }: { postId: string | undefined }) {
|
|
276
|
+
* // Use `isLoading`, not `isPending`, so the loading state doesn't show while the query is disabled.
|
|
277
|
+
* const { data, isLoading, isError, error } = useInfiniteQuery({
|
|
278
|
+
* queryKey: ['post', postId, 'comments'],
|
|
279
|
+
* queryFn:
|
|
280
|
+
* postId != null
|
|
281
|
+
* ? ({ pageParam }) => fetchComments(postId, pageParam)
|
|
282
|
+
* : skipToken,
|
|
283
|
+
* initialPageParam: 0,
|
|
284
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
285
|
+
* })
|
|
286
|
+
*
|
|
287
|
+
* if (postId == null) return 'Select a post'
|
|
288
|
+
* if (isLoading) return 'Loading...'
|
|
289
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
290
|
+
*
|
|
112
291
|
* return (
|
|
113
|
-
* <
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* >
|
|
117
|
-
* {isFetchingNextPage
|
|
118
|
-
* ? 'Loading more...'
|
|
119
|
-
* : hasNextPage
|
|
120
|
-
* ? 'Load More'
|
|
121
|
-
* : 'Nothing more to load'}
|
|
122
|
-
* </button>
|
|
292
|
+
* <ul>
|
|
293
|
+
* {data?.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}
|
|
294
|
+
* </ul>
|
|
123
295
|
* )
|
|
124
296
|
* }
|
|
125
297
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useInfiniteQuery.d.cts","names":[],"sources":["../../src/useInfiniteQuery.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useInfiniteQuery.d.cts","names":[],"sources":["../../src/useInfiniteQuery.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgEgB,iBACd,cACA,SAAS,cACT,QAAQ,aAAa,eACrB,kBAAkB,WAAW,UAC7B,sBAEA,SAAS,kCACP,cACA,QACA,OACA,WACA,aAEF,cAAc,cACb,8BAA8B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA+GxB,iBACd,cACA,SAAS,cACT,QAAQ,aAAa,eACrB,kBAAkB,WAAW,UAC7B,sBAEA,SAAS,oCACP,cACA,QACA,OACA,WACA,aAEF,cAAc,cACb,uBAAuB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA6IjB,iBACd,cACA,SAAS,cACT,QAAQ,aAAa,eACrB,kBAAkB,WAAW,UAC7B,sBAEA,SAAS,wBACP,cACA,QACA,OACA,WACA,aAEF,cAAc,cACb,uBAAuB,OAAO"}
|
|
@@ -3,7 +3,7 @@ import { DefinedInitialDataInfiniteOptions, UndefinedInitialDataInfiniteOptions
|
|
|
3
3
|
import { DefaultError, InfiniteData, QueryClient, QueryKey } from "@tanstack/query-core";
|
|
4
4
|
//#region src/useInfiniteQuery.d.ts
|
|
5
5
|
/**
|
|
6
|
-
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
6
|
+
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
7
7
|
* `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
|
|
8
8
|
*
|
|
9
9
|
* This overload is selected when `initialData` is set.
|
|
@@ -15,16 +15,19 @@ import { DefaultError, InfiniteData, QueryClient, QueryKey } from "@tanstack/que
|
|
|
15
15
|
* @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`, with `initialData` set.
|
|
16
16
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
17
17
|
* be used.
|
|
18
|
-
* @returns The same properties as `useQuery`, with the addition of `
|
|
19
|
-
* `
|
|
20
|
-
* `
|
|
18
|
+
* @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
|
|
19
|
+
* `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
|
|
20
|
+
* `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
|
|
21
|
+
* `InfiniteData<TQueryFnData>` shape.
|
|
21
22
|
*
|
|
22
23
|
* @example
|
|
23
24
|
* ```tsx
|
|
24
25
|
* import { useInfiniteQuery } from '@tanstack/preact-query'
|
|
25
26
|
*
|
|
26
27
|
* function Projects() {
|
|
27
|
-
*
|
|
28
|
+
* // `data` is never `undefined`, thanks to `initialData` — even if a refetch fails, so the
|
|
29
|
+
* // list stays visible alongside the error.
|
|
30
|
+
* const { data, isError, error } = useInfiniteQuery({
|
|
28
31
|
* queryKey: ['projects'],
|
|
29
32
|
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
30
33
|
* initialPageParam: 0,
|
|
@@ -32,13 +35,20 @@ import { DefaultError, InfiniteData, QueryClient, QueryKey } from "@tanstack/que
|
|
|
32
35
|
* initialData: { pages: [], pageParams: [] },
|
|
33
36
|
* })
|
|
34
37
|
*
|
|
35
|
-
* return
|
|
38
|
+
* return (
|
|
39
|
+
* <div>
|
|
40
|
+
* {isError ? <span>Error: {error.message}</span> : null}
|
|
41
|
+
* <ul>
|
|
42
|
+
* {data.pages.map((page) => page.projects.map((p) => <li key={p.id}>{p.name}</li>))}
|
|
43
|
+
* </ul>
|
|
44
|
+
* </div>
|
|
45
|
+
* )
|
|
36
46
|
* }
|
|
37
47
|
* ```
|
|
38
48
|
*/
|
|
39
49
|
declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(options: DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: QueryClient): DefinedUseInfiniteQueryResult<TData, TError>;
|
|
40
50
|
/**
|
|
41
|
-
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
51
|
+
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
42
52
|
* `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
|
|
43
53
|
*
|
|
44
54
|
* @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
|
|
@@ -48,16 +58,18 @@ declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = I
|
|
|
48
58
|
* @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`.
|
|
49
59
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
50
60
|
* be used.
|
|
51
|
-
* @returns The same properties as `useQuery`, with the addition of `
|
|
52
|
-
* `
|
|
53
|
-
* `
|
|
61
|
+
* @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
|
|
62
|
+
* `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
|
|
63
|
+
* `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
|
|
64
|
+
* `InfiniteData<TQueryFnData>` shape.
|
|
54
65
|
*
|
|
55
66
|
* @example
|
|
67
|
+
* Fetching the next page from a "Load More" button click:
|
|
56
68
|
* ```tsx
|
|
57
69
|
* import { useInfiniteQuery } from '@tanstack/preact-query'
|
|
58
70
|
*
|
|
59
71
|
* function Projects() {
|
|
60
|
-
* const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
|
|
72
|
+
* const { data, isPending, isError, error, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
|
|
61
73
|
* useInfiniteQuery({
|
|
62
74
|
* queryKey: ['projects'],
|
|
63
75
|
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
@@ -65,24 +77,88 @@ declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = I
|
|
|
65
77
|
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
66
78
|
* })
|
|
67
79
|
*
|
|
80
|
+
* if (isPending) return 'Loading...'
|
|
81
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
82
|
+
*
|
|
68
83
|
* return (
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
84
|
+
* <>
|
|
85
|
+
* <ul>
|
|
86
|
+
* {data.pages.map((page) =>
|
|
87
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
88
|
+
* )}
|
|
89
|
+
* </ul>
|
|
90
|
+
* <button
|
|
91
|
+
* onClick={() => fetchNextPage()}
|
|
92
|
+
* disabled={!hasNextPage || isFetching}
|
|
93
|
+
* >
|
|
94
|
+
* {isFetchingNextPage
|
|
95
|
+
* ? 'Loading more...'
|
|
96
|
+
* : hasNextPage
|
|
97
|
+
* ? 'Load More'
|
|
98
|
+
* : 'Nothing more to load'}
|
|
99
|
+
* </button>
|
|
100
|
+
* </>
|
|
101
|
+
* )
|
|
102
|
+
* }
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
|
|
107
|
+
* sentinel element after the list:
|
|
108
|
+
* ```tsx
|
|
109
|
+
* import { useInfiniteQuery } from '@tanstack/preact-query'
|
|
110
|
+
* import { useEffect, useRef } from 'preact/hooks'
|
|
111
|
+
*
|
|
112
|
+
* function Projects() {
|
|
113
|
+
* const {
|
|
114
|
+
* data,
|
|
115
|
+
* isPending,
|
|
116
|
+
* isError,
|
|
117
|
+
* error,
|
|
118
|
+
* fetchNextPage,
|
|
119
|
+
* hasNextPage,
|
|
120
|
+
* isFetching,
|
|
121
|
+
* isFetchingNextPage,
|
|
122
|
+
* } = useInfiniteQuery({
|
|
123
|
+
* queryKey: ['projects'],
|
|
124
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
125
|
+
* initialPageParam: 0,
|
|
126
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
127
|
+
* })
|
|
128
|
+
*
|
|
129
|
+
* const sentinelRef = useRef<HTMLDivElement>(null)
|
|
130
|
+
*
|
|
131
|
+
* useEffect(() => {
|
|
132
|
+
* const sentinel = sentinelRef.current
|
|
133
|
+
* if (sentinel == null || !hasNextPage || isFetching) return
|
|
134
|
+
*
|
|
135
|
+
* const observer = new IntersectionObserver(([entry]) => {
|
|
136
|
+
* if (entry?.isIntersecting) fetchNextPage()
|
|
137
|
+
* })
|
|
138
|
+
* observer.observe(sentinel)
|
|
139
|
+
*
|
|
140
|
+
* return () => observer.disconnect()
|
|
141
|
+
* }, [hasNextPage, isFetching, fetchNextPage])
|
|
142
|
+
*
|
|
143
|
+
* if (isPending) return 'Loading...'
|
|
144
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
145
|
+
*
|
|
146
|
+
* return (
|
|
147
|
+
* <>
|
|
148
|
+
* <ul>
|
|
149
|
+
* {data.pages.map((page) =>
|
|
150
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
151
|
+
* )}
|
|
152
|
+
* </ul>
|
|
153
|
+
* <div ref={sentinelRef}>{isFetchingNextPage ? 'Loading more...' : null}</div>
|
|
154
|
+
* </>
|
|
79
155
|
* )
|
|
80
156
|
* }
|
|
81
157
|
* ```
|
|
82
158
|
*/
|
|
83
159
|
declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(options: UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: QueryClient): UseInfiniteQueryResult<TData, TError>;
|
|
84
160
|
/**
|
|
85
|
-
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
161
|
+
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
86
162
|
* `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
|
|
87
163
|
*
|
|
88
164
|
* @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
|
|
@@ -92,16 +168,18 @@ declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = I
|
|
|
92
168
|
* @param options - The {@link UseInfiniteQueryOptions} to use — everything you can pass to `useInfiniteQuery`.
|
|
93
169
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
94
170
|
* be used.
|
|
95
|
-
* @returns The same properties as `useQuery`, with the addition of `
|
|
96
|
-
* `
|
|
97
|
-
* `
|
|
171
|
+
* @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
|
|
172
|
+
* `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
|
|
173
|
+
* `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
|
|
174
|
+
* `InfiniteData<TQueryFnData>` shape.
|
|
98
175
|
*
|
|
99
176
|
* @example
|
|
177
|
+
* Fetching the next page from a "Load More" button click:
|
|
100
178
|
* ```tsx
|
|
101
179
|
* import { useInfiniteQuery } from '@tanstack/preact-query'
|
|
102
180
|
*
|
|
103
181
|
* function Projects() {
|
|
104
|
-
* const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
|
|
182
|
+
* const { data, isPending, isError, error, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
|
|
105
183
|
* useInfiniteQuery({
|
|
106
184
|
* queryKey: ['projects'],
|
|
107
185
|
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
@@ -109,17 +187,111 @@ declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = I
|
|
|
109
187
|
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
110
188
|
* })
|
|
111
189
|
*
|
|
190
|
+
* if (isPending) return 'Loading...'
|
|
191
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
192
|
+
*
|
|
193
|
+
* return (
|
|
194
|
+
* <>
|
|
195
|
+
* <ul>
|
|
196
|
+
* {data.pages.map((page) =>
|
|
197
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
198
|
+
* )}
|
|
199
|
+
* </ul>
|
|
200
|
+
* <button
|
|
201
|
+
* onClick={() => fetchNextPage()}
|
|
202
|
+
* disabled={!hasNextPage || isFetching}
|
|
203
|
+
* >
|
|
204
|
+
* {isFetchingNextPage
|
|
205
|
+
* ? 'Loading more...'
|
|
206
|
+
* : hasNextPage
|
|
207
|
+
* ? 'Load More'
|
|
208
|
+
* : 'Nothing more to load'}
|
|
209
|
+
* </button>
|
|
210
|
+
* </>
|
|
211
|
+
* )
|
|
212
|
+
* }
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
|
|
217
|
+
* sentinel element after the list:
|
|
218
|
+
* ```tsx
|
|
219
|
+
* import { useInfiniteQuery } from '@tanstack/preact-query'
|
|
220
|
+
* import { useEffect, useRef } from 'preact/hooks'
|
|
221
|
+
*
|
|
222
|
+
* function Projects() {
|
|
223
|
+
* const {
|
|
224
|
+
* data,
|
|
225
|
+
* isPending,
|
|
226
|
+
* isError,
|
|
227
|
+
* error,
|
|
228
|
+
* fetchNextPage,
|
|
229
|
+
* hasNextPage,
|
|
230
|
+
* isFetching,
|
|
231
|
+
* isFetchingNextPage,
|
|
232
|
+
* } = useInfiniteQuery({
|
|
233
|
+
* queryKey: ['projects'],
|
|
234
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
235
|
+
* initialPageParam: 0,
|
|
236
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
237
|
+
* })
|
|
238
|
+
*
|
|
239
|
+
* const sentinelRef = useRef<HTMLDivElement>(null)
|
|
240
|
+
*
|
|
241
|
+
* useEffect(() => {
|
|
242
|
+
* const sentinel = sentinelRef.current
|
|
243
|
+
* if (sentinel == null || !hasNextPage || isFetching) return
|
|
244
|
+
*
|
|
245
|
+
* const observer = new IntersectionObserver(([entry]) => {
|
|
246
|
+
* if (entry?.isIntersecting) fetchNextPage()
|
|
247
|
+
* })
|
|
248
|
+
* observer.observe(sentinel)
|
|
249
|
+
*
|
|
250
|
+
* return () => observer.disconnect()
|
|
251
|
+
* }, [hasNextPage, isFetching, fetchNextPage])
|
|
252
|
+
*
|
|
253
|
+
* if (isPending) return 'Loading...'
|
|
254
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
255
|
+
*
|
|
256
|
+
* return (
|
|
257
|
+
* <>
|
|
258
|
+
* <ul>
|
|
259
|
+
* {data.pages.map((page) =>
|
|
260
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
261
|
+
* )}
|
|
262
|
+
* </ul>
|
|
263
|
+
* <div ref={sentinelRef}>{isFetchingNextPage ? 'Loading more...' : null}</div>
|
|
264
|
+
* </>
|
|
265
|
+
* )
|
|
266
|
+
* }
|
|
267
|
+
* ```
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* A query that's disabled, type safe, until `postId` is set — pass `skipToken` as `queryFn`
|
|
271
|
+
* instead of setting `enabled: false`:
|
|
272
|
+
* ```tsx
|
|
273
|
+
* import { skipToken, useInfiniteQuery } from '@tanstack/preact-query'
|
|
274
|
+
*
|
|
275
|
+
* function Comments({ postId }: { postId: string | undefined }) {
|
|
276
|
+
* // Use `isLoading`, not `isPending`, so the loading state doesn't show while the query is disabled.
|
|
277
|
+
* const { data, isLoading, isError, error } = useInfiniteQuery({
|
|
278
|
+
* queryKey: ['post', postId, 'comments'],
|
|
279
|
+
* queryFn:
|
|
280
|
+
* postId != null
|
|
281
|
+
* ? ({ pageParam }) => fetchComments(postId, pageParam)
|
|
282
|
+
* : skipToken,
|
|
283
|
+
* initialPageParam: 0,
|
|
284
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
285
|
+
* })
|
|
286
|
+
*
|
|
287
|
+
* if (postId == null) return 'Select a post'
|
|
288
|
+
* if (isLoading) return 'Loading...'
|
|
289
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
290
|
+
*
|
|
112
291
|
* return (
|
|
113
|
-
* <
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* >
|
|
117
|
-
* {isFetchingNextPage
|
|
118
|
-
* ? 'Loading more...'
|
|
119
|
-
* : hasNextPage
|
|
120
|
-
* ? 'Load More'
|
|
121
|
-
* : 'Nothing more to load'}
|
|
122
|
-
* </button>
|
|
292
|
+
* <ul>
|
|
293
|
+
* {data?.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}
|
|
294
|
+
* </ul>
|
|
123
295
|
* )
|
|
124
296
|
* }
|
|
125
297
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useInfiniteQuery.d.ts","names":[],"sources":["../../src/useInfiniteQuery.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useInfiniteQuery.d.ts","names":[],"sources":["../../src/useInfiniteQuery.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgEgB,iBACd,cACA,SAAS,cACT,QAAQ,aAAa,eACrB,kBAAkB,WAAW,UAC7B,sBAEA,SAAS,kCACP,cACA,QACA,OACA,WACA,aAEF,cAAc,cACb,8BAA8B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA+GxB,iBACd,cACA,SAAS,cACT,QAAQ,aAAa,eACrB,kBAAkB,WAAW,UAC7B,sBAEA,SAAS,oCACP,cACA,QACA,OACA,WACA,aAEF,cAAc,cACb,uBAAuB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA6IjB,iBACd,cACA,SAAS,cACT,QAAQ,aAAa,eACrB,kBAAkB,WAAW,UAC7B,sBAEA,SAAS,wBACP,cACA,QACA,OACA,WACA,aAEF,cAAc,cACb,uBAAuB,OAAO"}
|