@tanstack/react-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 +44 -1
- package/build/legacy/HydrationBoundary.cjs.map +1 -1
- package/build/legacy/HydrationBoundary.d.cts +61 -0
- package/build/legacy/HydrationBoundary.d.cts.map +1 -1
- package/build/legacy/HydrationBoundary.d.ts +61 -0
- package/build/legacy/HydrationBoundary.d.ts.map +1 -1
- package/build/legacy/HydrationBoundary.js +44 -1
- package/build/legacy/HydrationBoundary.js.map +1 -1
- package/build/legacy/IsRestoringProvider.cjs +11 -0
- package/build/legacy/IsRestoringProvider.cjs.map +1 -1
- package/build/legacy/IsRestoringProvider.d.cts +11 -0
- package/build/legacy/IsRestoringProvider.d.cts.map +1 -1
- package/build/legacy/IsRestoringProvider.d.ts +11 -0
- package/build/legacy/IsRestoringProvider.d.ts.map +1 -1
- package/build/legacy/IsRestoringProvider.js +11 -0
- package/build/legacy/IsRestoringProvider.js.map +1 -1
- package/build/legacy/QueryClientProvider.cjs +30 -0
- package/build/legacy/QueryClientProvider.cjs.map +1 -1
- package/build/legacy/QueryClientProvider.d.cts +41 -0
- package/build/legacy/QueryClientProvider.d.cts.map +1 -1
- package/build/legacy/QueryClientProvider.d.ts +41 -0
- package/build/legacy/QueryClientProvider.d.ts.map +1 -1
- package/build/legacy/QueryClientProvider.js +30 -0
- package/build/legacy/QueryClientProvider.js.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.cjs +76 -0
- package/build/legacy/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.d.cts +77 -0
- package/build/legacy/QueryErrorResetBoundary.d.cts.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.d.ts +77 -0
- package/build/legacy/QueryErrorResetBoundary.d.ts.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.js +76 -0
- package/build/legacy/QueryErrorResetBoundary.js.map +1 -1
- package/build/legacy/infiniteQueryOptions.cjs.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.cts +173 -0
- package/build/legacy/infiniteQueryOptions.d.cts.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.ts +173 -0
- 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 +55 -0
- package/build/legacy/mutationOptions.d.cts.map +1 -1
- package/build/legacy/mutationOptions.d.ts +55 -0
- 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 +175 -0
- package/build/legacy/queryOptions.d.cts.map +1 -1
- package/build/legacy/queryOptions.d.ts +175 -0
- package/build/legacy/queryOptions.d.ts.map +1 -1
- package/build/legacy/queryOptions.js.map +1 -1
- package/build/legacy/types.cjs.map +1 -1
- package/build/legacy/types.d.cts +228 -2
- package/build/legacy/types.d.cts.map +1 -1
- package/build/legacy/types.d.ts +228 -2
- package/build/legacy/types.d.ts.map +1 -1
- package/build/legacy/types.js.map +1 -1
- package/build/legacy/useInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useInfiniteQuery.d.cts +292 -0
- package/build/legacy/useInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useInfiniteQuery.d.ts +292 -0
- package/build/legacy/useInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useInfiniteQuery.js.map +1 -1
- package/build/legacy/useIsFetching.cjs +36 -0
- package/build/legacy/useIsFetching.cjs.map +1 -1
- package/build/legacy/useIsFetching.d.cts +36 -0
- package/build/legacy/useIsFetching.d.cts.map +1 -1
- package/build/legacy/useIsFetching.d.ts +36 -0
- package/build/legacy/useIsFetching.d.ts.map +1 -1
- package/build/legacy/useIsFetching.js +36 -0
- package/build/legacy/useIsFetching.js.map +1 -1
- package/build/legacy/useMutation.cjs +172 -0
- package/build/legacy/useMutation.cjs.map +1 -1
- package/build/legacy/useMutation.d.cts +172 -0
- package/build/legacy/useMutation.d.cts.map +1 -1
- package/build/legacy/useMutation.d.ts +172 -0
- package/build/legacy/useMutation.d.ts.map +1 -1
- package/build/legacy/useMutation.js +172 -0
- package/build/legacy/useMutation.js.map +1 -1
- package/build/legacy/useMutationState.cjs +94 -0
- package/build/legacy/useMutationState.cjs.map +1 -1
- package/build/legacy/useMutationState.d.cts +94 -0
- package/build/legacy/useMutationState.d.cts.map +1 -1
- package/build/legacy/useMutationState.d.ts +94 -0
- package/build/legacy/useMutationState.d.ts.map +1 -1
- package/build/legacy/useMutationState.js +94 -0
- package/build/legacy/useMutationState.js.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.cjs +44 -0
- package/build/legacy/usePrefetchInfiniteQuery.cjs.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.cts +44 -0
- package/build/legacy/usePrefetchInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.ts +44 -0
- package/build/legacy/usePrefetchInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.js +44 -0
- package/build/legacy/usePrefetchInfiniteQuery.js.map +1 -1
- package/build/legacy/usePrefetchQuery.cjs +35 -0
- package/build/legacy/usePrefetchQuery.cjs.map +1 -1
- package/build/legacy/usePrefetchQuery.d.cts +35 -0
- package/build/legacy/usePrefetchQuery.d.cts.map +1 -1
- package/build/legacy/usePrefetchQuery.d.ts +35 -0
- package/build/legacy/usePrefetchQuery.d.ts.map +1 -1
- package/build/legacy/usePrefetchQuery.js +35 -0
- package/build/legacy/usePrefetchQuery.js.map +1 -1
- package/build/legacy/useQueries.cjs +130 -0
- package/build/legacy/useQueries.cjs.map +1 -1
- package/build/legacy/useQueries.d.cts +165 -2
- package/build/legacy/useQueries.d.cts.map +1 -1
- package/build/legacy/useQueries.d.ts +165 -2
- package/build/legacy/useQueries.d.ts.map +1 -1
- package/build/legacy/useQueries.js +130 -0
- package/build/legacy/useQueries.js.map +1 -1
- package/build/legacy/useQuery.cjs.map +1 -1
- package/build/legacy/useQuery.d.cts +246 -0
- package/build/legacy/useQuery.d.cts.map +1 -1
- package/build/legacy/useQuery.d.ts +246 -0
- package/build/legacy/useQuery.d.ts.map +1 -1
- package/build/legacy/useQuery.js.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.cjs +86 -0
- package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.cts +86 -0
- package/build/legacy/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.ts +86 -0
- package/build/legacy/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.js +86 -0
- package/build/legacy/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/legacy/useSuspenseQueries.cjs.map +1 -1
- package/build/legacy/useSuspenseQueries.d.cts +320 -2
- package/build/legacy/useSuspenseQueries.d.cts.map +1 -1
- package/build/legacy/useSuspenseQueries.d.ts +320 -2
- package/build/legacy/useSuspenseQueries.d.ts.map +1 -1
- package/build/legacy/useSuspenseQueries.js.map +1 -1
- package/build/legacy/useSuspenseQuery.cjs +67 -0
- package/build/legacy/useSuspenseQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseQuery.d.cts +67 -0
- package/build/legacy/useSuspenseQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseQuery.d.ts +67 -0
- package/build/legacy/useSuspenseQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseQuery.js +67 -0
- package/build/legacy/useSuspenseQuery.js.map +1 -1
- package/build/modern/HydrationBoundary.cjs +44 -1
- package/build/modern/HydrationBoundary.cjs.map +1 -1
- package/build/modern/HydrationBoundary.d.cts +61 -0
- package/build/modern/HydrationBoundary.d.cts.map +1 -1
- package/build/modern/HydrationBoundary.d.ts +61 -0
- package/build/modern/HydrationBoundary.d.ts.map +1 -1
- package/build/modern/HydrationBoundary.js +44 -1
- package/build/modern/HydrationBoundary.js.map +1 -1
- package/build/modern/IsRestoringProvider.cjs +11 -0
- package/build/modern/IsRestoringProvider.cjs.map +1 -1
- package/build/modern/IsRestoringProvider.d.cts +11 -0
- package/build/modern/IsRestoringProvider.d.cts.map +1 -1
- package/build/modern/IsRestoringProvider.d.ts +11 -0
- package/build/modern/IsRestoringProvider.d.ts.map +1 -1
- package/build/modern/IsRestoringProvider.js +11 -0
- package/build/modern/IsRestoringProvider.js.map +1 -1
- package/build/modern/QueryClientProvider.cjs +30 -0
- package/build/modern/QueryClientProvider.cjs.map +1 -1
- package/build/modern/QueryClientProvider.d.cts +41 -0
- package/build/modern/QueryClientProvider.d.cts.map +1 -1
- package/build/modern/QueryClientProvider.d.ts +41 -0
- package/build/modern/QueryClientProvider.d.ts.map +1 -1
- package/build/modern/QueryClientProvider.js +30 -0
- package/build/modern/QueryClientProvider.js.map +1 -1
- package/build/modern/QueryErrorResetBoundary.cjs +76 -0
- package/build/modern/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/modern/QueryErrorResetBoundary.d.cts +77 -0
- package/build/modern/QueryErrorResetBoundary.d.cts.map +1 -1
- package/build/modern/QueryErrorResetBoundary.d.ts +77 -0
- package/build/modern/QueryErrorResetBoundary.d.ts.map +1 -1
- package/build/modern/QueryErrorResetBoundary.js +76 -0
- package/build/modern/QueryErrorResetBoundary.js.map +1 -1
- package/build/modern/infiniteQueryOptions.cjs.map +1 -1
- package/build/modern/infiniteQueryOptions.d.cts +173 -0
- package/build/modern/infiniteQueryOptions.d.cts.map +1 -1
- package/build/modern/infiniteQueryOptions.d.ts +173 -0
- 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 +55 -0
- package/build/modern/mutationOptions.d.cts.map +1 -1
- package/build/modern/mutationOptions.d.ts +55 -0
- 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 +175 -0
- package/build/modern/queryOptions.d.cts.map +1 -1
- package/build/modern/queryOptions.d.ts +175 -0
- package/build/modern/queryOptions.d.ts.map +1 -1
- package/build/modern/queryOptions.js.map +1 -1
- package/build/modern/types.cjs.map +1 -1
- package/build/modern/types.d.cts +228 -2
- package/build/modern/types.d.cts.map +1 -1
- package/build/modern/types.d.ts +228 -2
- package/build/modern/types.d.ts.map +1 -1
- package/build/modern/types.js.map +1 -1
- package/build/modern/useInfiniteQuery.cjs.map +1 -1
- package/build/modern/useInfiniteQuery.d.cts +292 -0
- package/build/modern/useInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useInfiniteQuery.d.ts +292 -0
- package/build/modern/useInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useInfiniteQuery.js.map +1 -1
- package/build/modern/useIsFetching.cjs +36 -0
- package/build/modern/useIsFetching.cjs.map +1 -1
- package/build/modern/useIsFetching.d.cts +36 -0
- package/build/modern/useIsFetching.d.cts.map +1 -1
- package/build/modern/useIsFetching.d.ts +36 -0
- package/build/modern/useIsFetching.d.ts.map +1 -1
- package/build/modern/useIsFetching.js +36 -0
- package/build/modern/useIsFetching.js.map +1 -1
- package/build/modern/useMutation.cjs +172 -0
- package/build/modern/useMutation.cjs.map +1 -1
- package/build/modern/useMutation.d.cts +172 -0
- package/build/modern/useMutation.d.cts.map +1 -1
- package/build/modern/useMutation.d.ts +172 -0
- package/build/modern/useMutation.d.ts.map +1 -1
- package/build/modern/useMutation.js +172 -0
- package/build/modern/useMutation.js.map +1 -1
- package/build/modern/useMutationState.cjs +94 -0
- package/build/modern/useMutationState.cjs.map +1 -1
- package/build/modern/useMutationState.d.cts +94 -0
- package/build/modern/useMutationState.d.cts.map +1 -1
- package/build/modern/useMutationState.d.ts +94 -0
- package/build/modern/useMutationState.d.ts.map +1 -1
- package/build/modern/useMutationState.js +94 -0
- package/build/modern/useMutationState.js.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.cjs +44 -0
- package/build/modern/usePrefetchInfiniteQuery.cjs.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.cts +44 -0
- package/build/modern/usePrefetchInfiniteQuery.d.cts.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.ts +44 -0
- package/build/modern/usePrefetchInfiniteQuery.d.ts.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.js +44 -0
- package/build/modern/usePrefetchInfiniteQuery.js.map +1 -1
- package/build/modern/usePrefetchQuery.cjs +35 -0
- package/build/modern/usePrefetchQuery.cjs.map +1 -1
- package/build/modern/usePrefetchQuery.d.cts +35 -0
- package/build/modern/usePrefetchQuery.d.cts.map +1 -1
- package/build/modern/usePrefetchQuery.d.ts +35 -0
- package/build/modern/usePrefetchQuery.d.ts.map +1 -1
- package/build/modern/usePrefetchQuery.js +35 -0
- package/build/modern/usePrefetchQuery.js.map +1 -1
- package/build/modern/useQueries.cjs +130 -0
- package/build/modern/useQueries.cjs.map +1 -1
- package/build/modern/useQueries.d.cts +165 -2
- package/build/modern/useQueries.d.cts.map +1 -1
- package/build/modern/useQueries.d.ts +165 -2
- package/build/modern/useQueries.d.ts.map +1 -1
- package/build/modern/useQueries.js +130 -0
- package/build/modern/useQueries.js.map +1 -1
- package/build/modern/useQuery.cjs.map +1 -1
- package/build/modern/useQuery.d.cts +246 -0
- package/build/modern/useQuery.d.cts.map +1 -1
- package/build/modern/useQuery.d.ts +246 -0
- package/build/modern/useQuery.d.ts.map +1 -1
- package/build/modern/useQuery.js.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.cjs +86 -0
- package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.cts +86 -0
- package/build/modern/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.ts +86 -0
- package/build/modern/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.js +86 -0
- package/build/modern/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/modern/useSuspenseQueries.cjs.map +1 -1
- package/build/modern/useSuspenseQueries.d.cts +320 -2
- package/build/modern/useSuspenseQueries.d.cts.map +1 -1
- package/build/modern/useSuspenseQueries.d.ts +320 -2
- package/build/modern/useSuspenseQueries.d.ts.map +1 -1
- package/build/modern/useSuspenseQueries.js.map +1 -1
- package/build/modern/useSuspenseQuery.cjs +67 -0
- package/build/modern/useSuspenseQuery.cjs.map +1 -1
- package/build/modern/useSuspenseQuery.d.cts +67 -0
- package/build/modern/useSuspenseQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseQuery.d.ts +67 -0
- package/build/modern/useSuspenseQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseQuery.js +67 -0
- 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 +61 -1
- package/src/IsRestoringProvider.ts +12 -0
- package/src/QueryClientProvider.tsx +41 -0
- package/src/QueryErrorResetBoundary.tsx +89 -0
- package/src/infiniteQueryOptions.ts +173 -0
- package/src/mutationOptions.ts +55 -0
- package/src/queryOptions.ts +175 -0
- package/src/types.ts +228 -2
- package/src/useInfiniteQuery.ts +292 -0
- package/src/useIsFetching.ts +36 -0
- package/src/useMutation.ts +172 -0
- package/src/useMutationState.ts +94 -0
- package/src/usePrefetchInfiniteQuery.tsx +44 -0
- package/src/usePrefetchQuery.tsx +35 -0
- package/src/useQueries.ts +165 -2
- package/src/useQuery.ts +246 -0
- package/src/useSuspenseInfiniteQuery.ts +86 -0
- package/src/useSuspenseQueries.ts +320 -2
- package/src/useSuspenseQuery.ts +67 -0
package/src/useInfiniteQuery.ts
CHANGED
|
@@ -18,6 +18,50 @@ import type {
|
|
|
18
18
|
UndefinedInitialDataInfiniteOptions,
|
|
19
19
|
} from './infiniteQueryOptions'
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
23
|
+
* `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
|
|
24
|
+
*
|
|
25
|
+
* This overload is selected when `initialData` is set.
|
|
26
|
+
*
|
|
27
|
+
* @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
|
|
28
|
+
* refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
|
|
29
|
+
* actions, or add conditions like `hasNextPage && !isFetching`.
|
|
30
|
+
* @see {@link infiniteQueryOptions} to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.
|
|
31
|
+
* @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`, with `initialData` set.
|
|
32
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
33
|
+
* be used.
|
|
34
|
+
* @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
|
|
35
|
+
* `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
|
|
36
|
+
* `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
|
|
37
|
+
* `InfiniteData<TQueryFnData>` shape.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* import { useInfiniteQuery } from '@tanstack/react-query'
|
|
42
|
+
*
|
|
43
|
+
* function Projects() {
|
|
44
|
+
* // `data` is never `undefined`, thanks to `initialData` — even if a refetch fails, so the
|
|
45
|
+
* // list stays visible alongside the error.
|
|
46
|
+
* const { data, isError, error } = useInfiniteQuery({
|
|
47
|
+
* queryKey: ['projects'],
|
|
48
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
49
|
+
* initialPageParam: 0,
|
|
50
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
51
|
+
* initialData: { pages: [], pageParams: [] },
|
|
52
|
+
* })
|
|
53
|
+
*
|
|
54
|
+
* return (
|
|
55
|
+
* <div>
|
|
56
|
+
* {isError ? <span>Error: {error.message}</span> : null}
|
|
57
|
+
* <ul>
|
|
58
|
+
* {data.pages.map((page) => page.projects.map((p) => <li key={p.id}>{p.name}</li>))}
|
|
59
|
+
* </ul>
|
|
60
|
+
* </div>
|
|
61
|
+
* )
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
21
65
|
export function useInfiniteQuery<
|
|
22
66
|
TQueryFnData,
|
|
23
67
|
TError = DefaultError,
|
|
@@ -35,6 +79,115 @@ export function useInfiniteQuery<
|
|
|
35
79
|
queryClient?: QueryClient,
|
|
36
80
|
): DefinedUseInfiniteQueryResult<TData, TError>
|
|
37
81
|
|
|
82
|
+
/**
|
|
83
|
+
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
84
|
+
* `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
|
|
85
|
+
*
|
|
86
|
+
* @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
|
|
87
|
+
* refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
|
|
88
|
+
* actions, or add conditions like `hasNextPage && !isFetching`.
|
|
89
|
+
* @see {@link infiniteQueryOptions} to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.
|
|
90
|
+
* @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`.
|
|
91
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
92
|
+
* be used.
|
|
93
|
+
* @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
|
|
94
|
+
* `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
|
|
95
|
+
* `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
|
|
96
|
+
* `InfiniteData<TQueryFnData>` shape.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* Fetching the next page from a "Load More" button click:
|
|
100
|
+
* ```tsx
|
|
101
|
+
* import { useInfiniteQuery } from '@tanstack/react-query'
|
|
102
|
+
*
|
|
103
|
+
* function Projects() {
|
|
104
|
+
* const { data, isPending, isError, error, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
|
|
105
|
+
* useInfiniteQuery({
|
|
106
|
+
* queryKey: ['projects'],
|
|
107
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
108
|
+
* initialPageParam: 0,
|
|
109
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
110
|
+
* })
|
|
111
|
+
*
|
|
112
|
+
* if (isPending) return 'Loading...'
|
|
113
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
114
|
+
*
|
|
115
|
+
* return (
|
|
116
|
+
* <>
|
|
117
|
+
* <ul>
|
|
118
|
+
* {data.pages.map((page) =>
|
|
119
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
120
|
+
* )}
|
|
121
|
+
* </ul>
|
|
122
|
+
* <button
|
|
123
|
+
* onClick={() => fetchNextPage()}
|
|
124
|
+
* disabled={!hasNextPage || isFetching}
|
|
125
|
+
* >
|
|
126
|
+
* {isFetchingNextPage
|
|
127
|
+
* ? 'Loading more...'
|
|
128
|
+
* : hasNextPage
|
|
129
|
+
* ? 'Load More'
|
|
130
|
+
* : 'Nothing more to load'}
|
|
131
|
+
* </button>
|
|
132
|
+
* </>
|
|
133
|
+
* )
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
|
|
139
|
+
* sentinel element after the list:
|
|
140
|
+
* ```tsx
|
|
141
|
+
* import { useInfiniteQuery } from '@tanstack/react-query'
|
|
142
|
+
* import { useEffect, useRef } from 'react'
|
|
143
|
+
*
|
|
144
|
+
* function Projects() {
|
|
145
|
+
* const {
|
|
146
|
+
* data,
|
|
147
|
+
* isPending,
|
|
148
|
+
* isError,
|
|
149
|
+
* error,
|
|
150
|
+
* fetchNextPage,
|
|
151
|
+
* hasNextPage,
|
|
152
|
+
* isFetching,
|
|
153
|
+
* isFetchingNextPage,
|
|
154
|
+
* } = useInfiniteQuery({
|
|
155
|
+
* queryKey: ['projects'],
|
|
156
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
157
|
+
* initialPageParam: 0,
|
|
158
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
159
|
+
* })
|
|
160
|
+
*
|
|
161
|
+
* const sentinelRef = useRef<HTMLDivElement>(null)
|
|
162
|
+
*
|
|
163
|
+
* useEffect(() => {
|
|
164
|
+
* const sentinel = sentinelRef.current
|
|
165
|
+
* if (sentinel == null || !hasNextPage || isFetching) return
|
|
166
|
+
*
|
|
167
|
+
* const observer = new IntersectionObserver(([entry]) => {
|
|
168
|
+
* if (entry?.isIntersecting) fetchNextPage()
|
|
169
|
+
* })
|
|
170
|
+
* observer.observe(sentinel)
|
|
171
|
+
*
|
|
172
|
+
* return () => observer.disconnect()
|
|
173
|
+
* }, [hasNextPage, isFetching, fetchNextPage])
|
|
174
|
+
*
|
|
175
|
+
* if (isPending) return 'Loading...'
|
|
176
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
177
|
+
*
|
|
178
|
+
* return (
|
|
179
|
+
* <>
|
|
180
|
+
* <ul>
|
|
181
|
+
* {data.pages.map((page) =>
|
|
182
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
183
|
+
* )}
|
|
184
|
+
* </ul>
|
|
185
|
+
* <div ref={sentinelRef}>{isFetchingNextPage ? 'Loading more...' : null}</div>
|
|
186
|
+
* </>
|
|
187
|
+
* )
|
|
188
|
+
* }
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
38
191
|
export function useInfiniteQuery<
|
|
39
192
|
TQueryFnData,
|
|
40
193
|
TError = DefaultError,
|
|
@@ -52,6 +205,145 @@ export function useInfiniteQuery<
|
|
|
52
205
|
queryClient?: QueryClient,
|
|
53
206
|
): UseInfiniteQueryResult<TData, TError>
|
|
54
207
|
|
|
208
|
+
/**
|
|
209
|
+
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
210
|
+
* `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
|
|
211
|
+
*
|
|
212
|
+
* @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
|
|
213
|
+
* refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
|
|
214
|
+
* actions, or add conditions like `hasNextPage && !isFetching`.
|
|
215
|
+
* @see {@link infiniteQueryOptions} to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.
|
|
216
|
+
* @param options - The {@link UseInfiniteQueryOptions} to use — everything you can pass to `useInfiniteQuery`.
|
|
217
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
218
|
+
* be used.
|
|
219
|
+
* @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
|
|
220
|
+
* `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
|
|
221
|
+
* `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
|
|
222
|
+
* `InfiniteData<TQueryFnData>` shape.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* Fetching the next page from a "Load More" button click:
|
|
226
|
+
* ```tsx
|
|
227
|
+
* import { useInfiniteQuery } from '@tanstack/react-query'
|
|
228
|
+
*
|
|
229
|
+
* function Projects() {
|
|
230
|
+
* const { data, isPending, isError, error, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
|
|
231
|
+
* useInfiniteQuery({
|
|
232
|
+
* queryKey: ['projects'],
|
|
233
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
234
|
+
* initialPageParam: 0,
|
|
235
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
236
|
+
* })
|
|
237
|
+
*
|
|
238
|
+
* if (isPending) return 'Loading...'
|
|
239
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
240
|
+
*
|
|
241
|
+
* return (
|
|
242
|
+
* <>
|
|
243
|
+
* <ul>
|
|
244
|
+
* {data.pages.map((page) =>
|
|
245
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
246
|
+
* )}
|
|
247
|
+
* </ul>
|
|
248
|
+
* <button
|
|
249
|
+
* onClick={() => fetchNextPage()}
|
|
250
|
+
* disabled={!hasNextPage || isFetching}
|
|
251
|
+
* >
|
|
252
|
+
* {isFetchingNextPage
|
|
253
|
+
* ? 'Loading more...'
|
|
254
|
+
* : hasNextPage
|
|
255
|
+
* ? 'Load More'
|
|
256
|
+
* : 'Nothing more to load'}
|
|
257
|
+
* </button>
|
|
258
|
+
* </>
|
|
259
|
+
* )
|
|
260
|
+
* }
|
|
261
|
+
* ```
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
|
|
265
|
+
* sentinel element after the list:
|
|
266
|
+
* ```tsx
|
|
267
|
+
* import { useInfiniteQuery } from '@tanstack/react-query'
|
|
268
|
+
* import { useEffect, useRef } from 'react'
|
|
269
|
+
*
|
|
270
|
+
* function Projects() {
|
|
271
|
+
* const {
|
|
272
|
+
* data,
|
|
273
|
+
* isPending,
|
|
274
|
+
* isError,
|
|
275
|
+
* error,
|
|
276
|
+
* fetchNextPage,
|
|
277
|
+
* hasNextPage,
|
|
278
|
+
* isFetching,
|
|
279
|
+
* isFetchingNextPage,
|
|
280
|
+
* } = useInfiniteQuery({
|
|
281
|
+
* queryKey: ['projects'],
|
|
282
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
283
|
+
* initialPageParam: 0,
|
|
284
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
285
|
+
* })
|
|
286
|
+
*
|
|
287
|
+
* const sentinelRef = useRef<HTMLDivElement>(null)
|
|
288
|
+
*
|
|
289
|
+
* useEffect(() => {
|
|
290
|
+
* const sentinel = sentinelRef.current
|
|
291
|
+
* if (sentinel == null || !hasNextPage || isFetching) return
|
|
292
|
+
*
|
|
293
|
+
* const observer = new IntersectionObserver(([entry]) => {
|
|
294
|
+
* if (entry?.isIntersecting) fetchNextPage()
|
|
295
|
+
* })
|
|
296
|
+
* observer.observe(sentinel)
|
|
297
|
+
*
|
|
298
|
+
* return () => observer.disconnect()
|
|
299
|
+
* }, [hasNextPage, isFetching, fetchNextPage])
|
|
300
|
+
*
|
|
301
|
+
* if (isPending) return 'Loading...'
|
|
302
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
303
|
+
*
|
|
304
|
+
* return (
|
|
305
|
+
* <>
|
|
306
|
+
* <ul>
|
|
307
|
+
* {data.pages.map((page) =>
|
|
308
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
309
|
+
* )}
|
|
310
|
+
* </ul>
|
|
311
|
+
* <div ref={sentinelRef}>{isFetchingNextPage ? 'Loading more...' : null}</div>
|
|
312
|
+
* </>
|
|
313
|
+
* )
|
|
314
|
+
* }
|
|
315
|
+
* ```
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* A query that's disabled, type safe, until `postId` is set — pass `skipToken` as `queryFn`
|
|
319
|
+
* instead of setting `enabled: false`:
|
|
320
|
+
* ```tsx
|
|
321
|
+
* import { skipToken, useInfiniteQuery } from '@tanstack/react-query'
|
|
322
|
+
*
|
|
323
|
+
* function Comments({ postId }: { postId: string | undefined }) {
|
|
324
|
+
* // Use `isLoading`, not `isPending`, so the loading state doesn't show while the query is disabled.
|
|
325
|
+
* const { data, isLoading, isError, error } = useInfiniteQuery({
|
|
326
|
+
* queryKey: ['post', postId, 'comments'],
|
|
327
|
+
* queryFn:
|
|
328
|
+
* postId != null
|
|
329
|
+
* ? ({ pageParam }) => fetchComments(postId, pageParam)
|
|
330
|
+
* : skipToken,
|
|
331
|
+
* initialPageParam: 0,
|
|
332
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
333
|
+
* })
|
|
334
|
+
*
|
|
335
|
+
* if (postId == null) return 'Select a post'
|
|
336
|
+
* if (isLoading) return 'Loading...'
|
|
337
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
338
|
+
*
|
|
339
|
+
* return (
|
|
340
|
+
* <ul>
|
|
341
|
+
* {data?.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}
|
|
342
|
+
* </ul>
|
|
343
|
+
* )
|
|
344
|
+
* }
|
|
345
|
+
* ```
|
|
346
|
+
*/
|
|
55
347
|
export function useInfiniteQuery<
|
|
56
348
|
TQueryFnData,
|
|
57
349
|
TError = DefaultError,
|
package/src/useIsFetching.ts
CHANGED
|
@@ -5,6 +5,42 @@ import { notifyManager } from '@tanstack/query-core'
|
|
|
5
5
|
import { useQueryClient } from './QueryClientProvider'
|
|
6
6
|
import type { QueryClient, QueryFilters } from '@tanstack/query-core'
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* The `useIsFetching` hook returns the `number` of the queries that your application is loading or fetching in
|
|
10
|
+
* the background (useful for app-wide loading indicators).
|
|
11
|
+
*
|
|
12
|
+
* @param filters - The {@link QueryFilters} to narrow down the matched queries.
|
|
13
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
14
|
+
* be used.
|
|
15
|
+
* @returns Will be the `number` of the queries that your application is currently loading or fetching in the
|
|
16
|
+
* background.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```tsx
|
|
20
|
+
* import { useIsFetching } from '@tanstack/react-query'
|
|
21
|
+
*
|
|
22
|
+
* function PostsFetchingIndicator() {
|
|
23
|
+
* // How many queries matching the posts prefix are fetching?
|
|
24
|
+
* const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
|
|
25
|
+
*
|
|
26
|
+
* return isFetchingPosts ? <span>Refreshing posts...</span> : null
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* A global loading indicator for any query fetching in the background, not just the ones on screen:
|
|
32
|
+
* ```tsx
|
|
33
|
+
* import { useIsFetching } from '@tanstack/react-query'
|
|
34
|
+
*
|
|
35
|
+
* function GlobalLoadingIndicator() {
|
|
36
|
+
* const isFetching = useIsFetching()
|
|
37
|
+
*
|
|
38
|
+
* return isFetching ? (
|
|
39
|
+
* <div>Queries are fetching in the background...</div>
|
|
40
|
+
* ) : null
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
8
44
|
export function useIsFetching(
|
|
9
45
|
filters?: QueryFilters,
|
|
10
46
|
queryClient?: QueryClient,
|
package/src/useMutation.ts
CHANGED
|
@@ -16,6 +16,178 @@ import type { DefaultError, QueryClient } from '@tanstack/query-core'
|
|
|
16
16
|
|
|
17
17
|
// HOOK
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
|
|
21
|
+
* `useMutation` is the hook for that.
|
|
22
|
+
*
|
|
23
|
+
* @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look
|
|
24
|
+
* the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
|
|
25
|
+
* @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.
|
|
26
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
27
|
+
* be used.
|
|
28
|
+
* @returns `mutate`/`mutateAsync` also accept per-call `onSuccess`/`onError`/`onSettled` callbacks as a second
|
|
29
|
+
* argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared
|
|
30
|
+
* mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call callbacks
|
|
31
|
+
* fire only for the latest call you've made, and only while the component is still mounted — unmounting before
|
|
32
|
+
* the mutation settles removes the subscription and prevents them from firing.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
37
|
+
*
|
|
38
|
+
* function AddTodo() {
|
|
39
|
+
* const queryClient = useQueryClient()
|
|
40
|
+
*
|
|
41
|
+
* const addMutation = useMutation({
|
|
42
|
+
* mutationFn: addTodo,
|
|
43
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
44
|
+
* })
|
|
45
|
+
*
|
|
46
|
+
* return (
|
|
47
|
+
* <button
|
|
48
|
+
* onClick={() =>
|
|
49
|
+
* addMutation.mutate('Item', {
|
|
50
|
+
* onError: (error) => console.error('Failed to add item:', error),
|
|
51
|
+
* })
|
|
52
|
+
* }
|
|
53
|
+
* >
|
|
54
|
+
* Add
|
|
55
|
+
* </button>
|
|
56
|
+
* )
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* Rendering the mutation's own state, rather than just firing it off:
|
|
62
|
+
* ```tsx
|
|
63
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
64
|
+
*
|
|
65
|
+
* function AddTodo() {
|
|
66
|
+
* const queryClient = useQueryClient()
|
|
67
|
+
*
|
|
68
|
+
* const addMutation = useMutation({
|
|
69
|
+
* mutationFn: addTodo,
|
|
70
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
71
|
+
* })
|
|
72
|
+
*
|
|
73
|
+
* return (
|
|
74
|
+
* <div>
|
|
75
|
+
* {addMutation.isPending ? (
|
|
76
|
+
* 'Adding todo...'
|
|
77
|
+
* ) : (
|
|
78
|
+
* <>
|
|
79
|
+
* {addMutation.isError ? (
|
|
80
|
+
* <div>An error occurred: {addMutation.error.message}</div>
|
|
81
|
+
* ) : null}
|
|
82
|
+
* <button onClick={() => addMutation.mutate('Item')}>Add</button>
|
|
83
|
+
* </>
|
|
84
|
+
* )}
|
|
85
|
+
* </div>
|
|
86
|
+
* )
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* Optimistic update via `onMutate`, rolling back on `onError`:
|
|
92
|
+
* ```tsx
|
|
93
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
94
|
+
*
|
|
95
|
+
* function AddTodo() {
|
|
96
|
+
* const queryClient = useQueryClient()
|
|
97
|
+
*
|
|
98
|
+
* const addMutation = useMutation({
|
|
99
|
+
* mutationFn: addTodo,
|
|
100
|
+
* onMutate: async (newTodo) => {
|
|
101
|
+
* await queryClient.cancelQueries({ queryKey: ['todos'] })
|
|
102
|
+
* const previousTodos = queryClient.getQueryData<Array<string>>(['todos'])
|
|
103
|
+
*
|
|
104
|
+
* queryClient.setQueryData<Array<string>>(['todos'], (old) => [
|
|
105
|
+
* ...(old ?? []),
|
|
106
|
+
* newTodo,
|
|
107
|
+
* ])
|
|
108
|
+
*
|
|
109
|
+
* // Passed to `onError` as `onMutateResult` if the mutation fails.
|
|
110
|
+
* return { previousTodos }
|
|
111
|
+
* },
|
|
112
|
+
* onError: (_err, _newTodo, onMutateResult) => {
|
|
113
|
+
* queryClient.setQueryData(['todos'], onMutateResult?.previousTodos)
|
|
114
|
+
* },
|
|
115
|
+
* onSettled: () => {
|
|
116
|
+
* queryClient.invalidateQueries({ queryKey: ['todos'] })
|
|
117
|
+
* },
|
|
118
|
+
* })
|
|
119
|
+
*
|
|
120
|
+
* return (
|
|
121
|
+
* <button onClick={() => addMutation.mutate('Item')}>Add</button>
|
|
122
|
+
* )
|
|
123
|
+
* }
|
|
124
|
+
* ```
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
|
|
128
|
+
* promise per call instead, so you can wait for all of them when they succeed:
|
|
129
|
+
* ```tsx
|
|
130
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
131
|
+
*
|
|
132
|
+
* function AddTodos() {
|
|
133
|
+
* const queryClient = useQueryClient()
|
|
134
|
+
*
|
|
135
|
+
* const addMutation = useMutation({
|
|
136
|
+
* mutationFn: addTodo,
|
|
137
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
138
|
+
* })
|
|
139
|
+
*
|
|
140
|
+
* async function handleAddAll(todos: Array<string>) {
|
|
141
|
+
* try {
|
|
142
|
+
* await Promise.all(todos.map((todo) => addMutation.mutateAsync(todo)))
|
|
143
|
+
* } catch (error) {
|
|
144
|
+
* console.error('Failed to add todos:', error)
|
|
145
|
+
* }
|
|
146
|
+
* }
|
|
147
|
+
*
|
|
148
|
+
* return (
|
|
149
|
+
* <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>
|
|
150
|
+
* Add all
|
|
151
|
+
* </button>
|
|
152
|
+
* )
|
|
153
|
+
* }
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* If some of the mutations above can fail independently of the others, and you want to know which ones
|
|
158
|
+
* did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
|
|
159
|
+
* `Promise.allSettled`:
|
|
160
|
+
* ```tsx
|
|
161
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
162
|
+
*
|
|
163
|
+
* function AddTodos() {
|
|
164
|
+
* const queryClient = useQueryClient()
|
|
165
|
+
*
|
|
166
|
+
* const addMutation = useMutation({
|
|
167
|
+
* mutationFn: addTodo,
|
|
168
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
169
|
+
* })
|
|
170
|
+
*
|
|
171
|
+
* async function handleAddAll(todos: Array<string>) {
|
|
172
|
+
* const addResults = await Promise.allSettled(
|
|
173
|
+
* todos.map((todo) => addMutation.mutateAsync(todo)),
|
|
174
|
+
* )
|
|
175
|
+
*
|
|
176
|
+
* addResults.forEach((addResult, index) => {
|
|
177
|
+
* if (addResult.status === 'rejected') {
|
|
178
|
+
* console.error(`Failed to add "${todos[index]}":`, addResult.reason)
|
|
179
|
+
* }
|
|
180
|
+
* })
|
|
181
|
+
* }
|
|
182
|
+
*
|
|
183
|
+
* return (
|
|
184
|
+
* <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>
|
|
185
|
+
* Add all
|
|
186
|
+
* </button>
|
|
187
|
+
* )
|
|
188
|
+
* }
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
19
191
|
export function useMutation<
|
|
20
192
|
TData = unknown,
|
|
21
193
|
TError = DefaultError,
|
package/src/useMutationState.ts
CHANGED
|
@@ -11,6 +11,27 @@ import type {
|
|
|
11
11
|
QueryClient,
|
|
12
12
|
} from '@tanstack/query-core'
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`
|
|
16
|
+
* (useful for app-wide loading indicators).
|
|
17
|
+
*
|
|
18
|
+
* @param filters - The {@link MutationFilters} to narrow down the matched mutations.
|
|
19
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
20
|
+
* be used.
|
|
21
|
+
* @returns Will be the `number` of the mutations that your application currently has `pending`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```tsx
|
|
25
|
+
* import { useIsMutating } from '@tanstack/react-query'
|
|
26
|
+
*
|
|
27
|
+
* function PostsMutatingIndicator() {
|
|
28
|
+
* // How many mutations matching the posts prefix are in progress?
|
|
29
|
+
* const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })
|
|
30
|
+
*
|
|
31
|
+
* return isMutatingPosts ? <span>Saving posts...</span> : null
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
14
35
|
export function useIsMutating(
|
|
15
36
|
filters?: MutationFilters,
|
|
16
37
|
queryClient?: QueryClient,
|
|
@@ -60,6 +81,79 @@ function getResult<
|
|
|
60
81
|
)
|
|
61
82
|
}
|
|
62
83
|
|
|
84
|
+
/**
|
|
85
|
+
* `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass
|
|
86
|
+
* `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
|
|
87
|
+
* state.
|
|
88
|
+
*
|
|
89
|
+
* @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
|
|
90
|
+
* mutation state.
|
|
91
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
92
|
+
* be used.
|
|
93
|
+
* @returns Will be an Array of whatever `select` returns for each matching mutation.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* Get all variables of all running mutations:
|
|
97
|
+
* ```tsx
|
|
98
|
+
* import { useMutationState } from '@tanstack/react-query'
|
|
99
|
+
*
|
|
100
|
+
* function PendingPosts() {
|
|
101
|
+
* const pendingVariables = useMutationState({
|
|
102
|
+
* filters: { status: 'pending' },
|
|
103
|
+
* select: (mutation) => mutation.state.variables,
|
|
104
|
+
* })
|
|
105
|
+
*
|
|
106
|
+
* return <>{pendingVariables.length} posts saving...</>
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* Get all data for specific mutations via the `mutationKey`:
|
|
112
|
+
* ```tsx
|
|
113
|
+
* import { useMutation, useMutationState } from '@tanstack/react-query'
|
|
114
|
+
*
|
|
115
|
+
* const mutationKey = ['posts']
|
|
116
|
+
*
|
|
117
|
+
* function Posts() {
|
|
118
|
+
* // Some mutation that we want to get the state for
|
|
119
|
+
* const mutation = useMutation({
|
|
120
|
+
* mutationKey,
|
|
121
|
+
* mutationFn: createPosts,
|
|
122
|
+
* })
|
|
123
|
+
*
|
|
124
|
+
* const savedPosts = useMutationState({
|
|
125
|
+
* // this mutation key needs to match the mutation key of the given mutation (see above)
|
|
126
|
+
* filters: { mutationKey, status: 'success' },
|
|
127
|
+
* select: (mutation) => mutation.state.data,
|
|
128
|
+
* })
|
|
129
|
+
*
|
|
130
|
+
* return (
|
|
131
|
+
* <button onClick={() => mutation.mutate(['New Post'])}>
|
|
132
|
+
* Create post ({savedPosts.length} saved so far)
|
|
133
|
+
* </button>
|
|
134
|
+
* )
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new
|
|
140
|
+
* entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the
|
|
141
|
+
* last item that `useMutationState` returns to get the latest successful invocation:
|
|
142
|
+
* ```tsx
|
|
143
|
+
* import { useMutationState } from '@tanstack/react-query'
|
|
144
|
+
*
|
|
145
|
+
* function LatestPost() {
|
|
146
|
+
* const savedPosts = useMutationState({
|
|
147
|
+
* filters: { mutationKey: ['posts'], status: 'success' },
|
|
148
|
+
* select: (mutation) => mutation.state.data,
|
|
149
|
+
* })
|
|
150
|
+
*
|
|
151
|
+
* const latestSavedPost = savedPosts[savedPosts.length - 1]
|
|
152
|
+
*
|
|
153
|
+
* return <>{latestSavedPost ? 'Saved' : 'Nothing saved yet'}</>
|
|
154
|
+
* }
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
63
157
|
export function useMutationState<
|
|
64
158
|
TResult = MutationState,
|
|
65
159
|
TMutation extends Mutation<any, any, any, any> =
|
|
@@ -9,6 +9,50 @@ import type {
|
|
|
9
9
|
} from '@tanstack/query-core'
|
|
10
10
|
import type { UsePrefetchInfiniteQueryOptions } from './types'
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* `usePrefetchInfiniteQuery` does not return anything, it should be used just to fire a prefetch during render,
|
|
14
|
+
* before a suspense boundary that wraps a component that uses `useSuspenseInfiniteQuery`. You can pass
|
|
15
|
+
* everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`, though
|
|
16
|
+
* `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required unless
|
|
17
|
+
* a default query function has been defined.
|
|
18
|
+
*
|
|
19
|
+
* `getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,
|
|
20
|
+
* as well as pageParam information, and should return a single variable that will be passed to your query
|
|
21
|
+
* function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.
|
|
22
|
+
*
|
|
23
|
+
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
24
|
+
* over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
|
|
25
|
+
* already there or already in flight.
|
|
26
|
+
*
|
|
27
|
+
* @param options - The {@link UsePrefetchInfiniteQueryOptions} to use — everything you can pass to `queryClient.infiniteQuery`.
|
|
28
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
29
|
+
* be used.
|
|
30
|
+
* @returns `void` — nothing is returned.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* import { Suspense } from 'react'
|
|
35
|
+
* import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/react-query'
|
|
36
|
+
*
|
|
37
|
+
* const projectsOptions = infiniteQueryOptions({
|
|
38
|
+
* queryKey: ['projects'],
|
|
39
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
40
|
+
* initialPageParam: 0,
|
|
41
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
42
|
+
* })
|
|
43
|
+
*
|
|
44
|
+
* function App() {
|
|
45
|
+
* // Fire the prefetch during render, before the suspense boundary below.
|
|
46
|
+
* usePrefetchInfiniteQuery(projectsOptions)
|
|
47
|
+
*
|
|
48
|
+
* return (
|
|
49
|
+
* <Suspense fallback={<h1>Loading projects...</h1>}>
|
|
50
|
+
* <Projects />
|
|
51
|
+
* </Suspense>
|
|
52
|
+
* )
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
12
56
|
export function usePrefetchInfiniteQuery<
|
|
13
57
|
TQueryFnData = unknown,
|
|
14
58
|
TError = DefaultError,
|