@tanstack/react-query 5.104.1 → 5.104.3
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 +2 -3
- package/build/legacy/HydrationBoundary.cjs.map +1 -1
- package/build/legacy/HydrationBoundary.d.cts +2 -3
- package/build/legacy/HydrationBoundary.d.cts.map +1 -1
- package/build/legacy/HydrationBoundary.d.ts +2 -3
- package/build/legacy/HydrationBoundary.d.ts.map +1 -1
- package/build/legacy/HydrationBoundary.js +2 -3
- package/build/legacy/HydrationBoundary.js.map +1 -1
- package/build/legacy/IsRestoringProvider.cjs +0 -1
- package/build/legacy/IsRestoringProvider.cjs.map +1 -1
- package/build/legacy/IsRestoringProvider.d.cts +0 -1
- package/build/legacy/IsRestoringProvider.d.cts.map +1 -1
- package/build/legacy/IsRestoringProvider.d.ts +0 -1
- package/build/legacy/IsRestoringProvider.d.ts.map +1 -1
- package/build/legacy/IsRestoringProvider.js +0 -1
- package/build/legacy/IsRestoringProvider.js.map +1 -1
- package/build/legacy/QueryClientProvider.cjs +3 -4
- package/build/legacy/QueryClientProvider.cjs.map +1 -1
- package/build/legacy/QueryClientProvider.d.cts +3 -4
- package/build/legacy/QueryClientProvider.d.cts.map +1 -1
- package/build/legacy/QueryClientProvider.d.ts +3 -4
- package/build/legacy/QueryClientProvider.d.ts.map +1 -1
- package/build/legacy/QueryClientProvider.js +3 -4
- package/build/legacy/QueryClientProvider.js.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.cjs +5 -5
- package/build/legacy/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.d.cts +23 -5
- package/build/legacy/QueryErrorResetBoundary.d.cts.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.d.ts +23 -5
- package/build/legacy/QueryErrorResetBoundary.d.ts.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.js +5 -5
- package/build/legacy/QueryErrorResetBoundary.js.map +1 -1
- package/build/legacy/errorBoundaryUtils.cjs +21 -0
- package/build/legacy/errorBoundaryUtils.cjs.map +1 -1
- package/build/legacy/errorBoundaryUtils.d.cts +21 -0
- package/build/legacy/errorBoundaryUtils.d.cts.map +1 -1
- package/build/legacy/errorBoundaryUtils.d.ts +21 -0
- package/build/legacy/errorBoundaryUtils.d.ts.map +1 -1
- package/build/legacy/errorBoundaryUtils.js +21 -0
- package/build/legacy/errorBoundaryUtils.js.map +1 -1
- package/build/legacy/infiniteQueryOptions.cjs.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.cts +5 -16
- package/build/legacy/infiniteQueryOptions.d.cts.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.ts +5 -16
- 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 +2 -6
- package/build/legacy/mutationOptions.d.cts.map +1 -1
- package/build/legacy/mutationOptions.d.ts +2 -6
- 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 +6 -16
- package/build/legacy/queryOptions.d.cts.map +1 -1
- package/build/legacy/queryOptions.d.ts +6 -16
- package/build/legacy/queryOptions.d.ts.map +1 -1
- package/build/legacy/queryOptions.js.map +1 -1
- package/build/legacy/suspense.cjs +27 -0
- package/build/legacy/suspense.cjs.map +1 -1
- package/build/legacy/suspense.d.cts +27 -0
- package/build/legacy/suspense.d.cts.map +1 -1
- package/build/legacy/suspense.d.ts +27 -0
- package/build/legacy/suspense.d.ts.map +1 -1
- package/build/legacy/suspense.js +27 -0
- package/build/legacy/suspense.js.map +1 -1
- package/build/legacy/types.cjs.map +1 -1
- package/build/legacy/types.d.cts +2 -22
- package/build/legacy/types.d.cts.map +1 -1
- package/build/legacy/types.d.ts +2 -22
- package/build/legacy/types.d.ts.map +1 -1
- package/build/legacy/types.js.map +1 -1
- package/build/legacy/useBaseQuery.cjs +13 -0
- package/build/legacy/useBaseQuery.cjs.map +1 -1
- package/build/legacy/useBaseQuery.d.cts +13 -0
- package/build/legacy/useBaseQuery.d.cts.map +1 -1
- package/build/legacy/useBaseQuery.d.ts +13 -0
- package/build/legacy/useBaseQuery.d.ts.map +1 -1
- package/build/legacy/useBaseQuery.js +13 -0
- package/build/legacy/useBaseQuery.js.map +1 -1
- package/build/legacy/useInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useInfiniteQuery.d.cts +12 -21
- package/build/legacy/useInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useInfiniteQuery.d.ts +12 -21
- package/build/legacy/useInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useInfiniteQuery.js.map +1 -1
- package/build/legacy/useIsFetching.cjs +0 -3
- package/build/legacy/useIsFetching.cjs.map +1 -1
- package/build/legacy/useIsFetching.d.cts +0 -3
- package/build/legacy/useIsFetching.d.cts.map +1 -1
- package/build/legacy/useIsFetching.d.ts +0 -3
- package/build/legacy/useIsFetching.d.ts.map +1 -1
- package/build/legacy/useIsFetching.js +0 -3
- package/build/legacy/useIsFetching.js.map +1 -1
- package/build/legacy/useMutation.cjs +4 -8
- package/build/legacy/useMutation.cjs.map +1 -1
- package/build/legacy/useMutation.d.cts +4 -8
- package/build/legacy/useMutation.d.cts.map +1 -1
- package/build/legacy/useMutation.d.ts +4 -8
- package/build/legacy/useMutation.d.ts.map +1 -1
- package/build/legacy/useMutation.js +4 -8
- package/build/legacy/useMutation.js.map +1 -1
- package/build/legacy/useMutationState.cjs +7 -6
- package/build/legacy/useMutationState.cjs.map +1 -1
- package/build/legacy/useMutationState.d.cts +0 -6
- package/build/legacy/useMutationState.d.cts.map +1 -1
- package/build/legacy/useMutationState.d.ts +0 -6
- package/build/legacy/useMutationState.d.ts.map +1 -1
- package/build/legacy/useMutationState.js +7 -6
- package/build/legacy/useMutationState.js.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.cjs +0 -3
- package/build/legacy/usePrefetchInfiniteQuery.cjs.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.cts +0 -3
- package/build/legacy/usePrefetchInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.ts +0 -3
- package/build/legacy/usePrefetchInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.js +0 -3
- package/build/legacy/usePrefetchInfiniteQuery.js.map +1 -1
- package/build/legacy/usePrefetchQuery.cjs +0 -3
- package/build/legacy/usePrefetchQuery.cjs.map +1 -1
- package/build/legacy/usePrefetchQuery.d.cts +0 -3
- package/build/legacy/usePrefetchQuery.d.cts.map +1 -1
- package/build/legacy/usePrefetchQuery.d.ts +0 -3
- package/build/legacy/usePrefetchQuery.d.ts.map +1 -1
- package/build/legacy/usePrefetchQuery.js +0 -3
- package/build/legacy/usePrefetchQuery.js.map +1 -1
- package/build/legacy/useQueries.cjs +7 -8
- package/build/legacy/useQueries.cjs.map +1 -1
- package/build/legacy/useQueries.d.cts +7 -11
- package/build/legacy/useQueries.d.cts.map +1 -1
- package/build/legacy/useQueries.d.ts +7 -11
- package/build/legacy/useQueries.d.ts.map +1 -1
- package/build/legacy/useQueries.js +7 -8
- package/build/legacy/useQueries.js.map +1 -1
- package/build/legacy/useQuery.cjs.map +1 -1
- package/build/legacy/useQuery.d.cts +8 -13
- package/build/legacy/useQuery.d.cts.map +1 -1
- package/build/legacy/useQuery.d.ts +8 -13
- package/build/legacy/useQuery.d.ts.map +1 -1
- package/build/legacy/useQuery.js.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.cjs +6 -8
- package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.cts +6 -8
- package/build/legacy/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.ts +6 -8
- package/build/legacy/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.js +6 -8
- package/build/legacy/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/legacy/useSuspenseQueries.cjs.map +1 -1
- package/build/legacy/useSuspenseQueries.d.cts +0 -9
- package/build/legacy/useSuspenseQueries.d.cts.map +1 -1
- package/build/legacy/useSuspenseQueries.d.ts +0 -9
- package/build/legacy/useSuspenseQueries.d.ts.map +1 -1
- package/build/legacy/useSuspenseQueries.js.map +1 -1
- package/build/legacy/useSuspenseQuery.cjs +4 -6
- package/build/legacy/useSuspenseQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseQuery.d.cts +4 -6
- package/build/legacy/useSuspenseQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseQuery.d.ts +4 -6
- package/build/legacy/useSuspenseQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseQuery.js +4 -6
- package/build/legacy/useSuspenseQuery.js.map +1 -1
- package/build/modern/HydrationBoundary.cjs +2 -3
- package/build/modern/HydrationBoundary.cjs.map +1 -1
- package/build/modern/HydrationBoundary.d.cts +2 -3
- package/build/modern/HydrationBoundary.d.cts.map +1 -1
- package/build/modern/HydrationBoundary.d.ts +2 -3
- package/build/modern/HydrationBoundary.d.ts.map +1 -1
- package/build/modern/HydrationBoundary.js +2 -3
- package/build/modern/HydrationBoundary.js.map +1 -1
- package/build/modern/IsRestoringProvider.cjs +0 -1
- package/build/modern/IsRestoringProvider.cjs.map +1 -1
- package/build/modern/IsRestoringProvider.d.cts +0 -1
- package/build/modern/IsRestoringProvider.d.cts.map +1 -1
- package/build/modern/IsRestoringProvider.d.ts +0 -1
- package/build/modern/IsRestoringProvider.d.ts.map +1 -1
- package/build/modern/IsRestoringProvider.js +0 -1
- package/build/modern/IsRestoringProvider.js.map +1 -1
- package/build/modern/QueryClientProvider.cjs +3 -4
- package/build/modern/QueryClientProvider.cjs.map +1 -1
- package/build/modern/QueryClientProvider.d.cts +3 -4
- package/build/modern/QueryClientProvider.d.cts.map +1 -1
- package/build/modern/QueryClientProvider.d.ts +3 -4
- package/build/modern/QueryClientProvider.d.ts.map +1 -1
- package/build/modern/QueryClientProvider.js +3 -4
- package/build/modern/QueryClientProvider.js.map +1 -1
- package/build/modern/QueryErrorResetBoundary.cjs +5 -5
- package/build/modern/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/modern/QueryErrorResetBoundary.d.cts +23 -5
- package/build/modern/QueryErrorResetBoundary.d.cts.map +1 -1
- package/build/modern/QueryErrorResetBoundary.d.ts +23 -5
- package/build/modern/QueryErrorResetBoundary.d.ts.map +1 -1
- package/build/modern/QueryErrorResetBoundary.js +5 -5
- package/build/modern/QueryErrorResetBoundary.js.map +1 -1
- package/build/modern/errorBoundaryUtils.cjs +21 -0
- package/build/modern/errorBoundaryUtils.cjs.map +1 -1
- package/build/modern/errorBoundaryUtils.d.cts +21 -0
- package/build/modern/errorBoundaryUtils.d.cts.map +1 -1
- package/build/modern/errorBoundaryUtils.d.ts +21 -0
- package/build/modern/errorBoundaryUtils.d.ts.map +1 -1
- package/build/modern/errorBoundaryUtils.js +21 -0
- package/build/modern/errorBoundaryUtils.js.map +1 -1
- package/build/modern/infiniteQueryOptions.cjs.map +1 -1
- package/build/modern/infiniteQueryOptions.d.cts +5 -16
- package/build/modern/infiniteQueryOptions.d.cts.map +1 -1
- package/build/modern/infiniteQueryOptions.d.ts +5 -16
- 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 +2 -6
- package/build/modern/mutationOptions.d.cts.map +1 -1
- package/build/modern/mutationOptions.d.ts +2 -6
- 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 +6 -16
- package/build/modern/queryOptions.d.cts.map +1 -1
- package/build/modern/queryOptions.d.ts +6 -16
- package/build/modern/queryOptions.d.ts.map +1 -1
- package/build/modern/queryOptions.js.map +1 -1
- package/build/modern/suspense.cjs +27 -0
- package/build/modern/suspense.cjs.map +1 -1
- package/build/modern/suspense.d.cts +27 -0
- package/build/modern/suspense.d.cts.map +1 -1
- package/build/modern/suspense.d.ts +27 -0
- package/build/modern/suspense.d.ts.map +1 -1
- package/build/modern/suspense.js +27 -0
- package/build/modern/suspense.js.map +1 -1
- package/build/modern/types.cjs.map +1 -1
- package/build/modern/types.d.cts +2 -22
- package/build/modern/types.d.cts.map +1 -1
- package/build/modern/types.d.ts +2 -22
- package/build/modern/types.d.ts.map +1 -1
- package/build/modern/types.js.map +1 -1
- package/build/modern/useBaseQuery.cjs +13 -0
- package/build/modern/useBaseQuery.cjs.map +1 -1
- package/build/modern/useBaseQuery.d.cts +13 -0
- package/build/modern/useBaseQuery.d.cts.map +1 -1
- package/build/modern/useBaseQuery.d.ts +13 -0
- package/build/modern/useBaseQuery.d.ts.map +1 -1
- package/build/modern/useBaseQuery.js +13 -0
- package/build/modern/useBaseQuery.js.map +1 -1
- package/build/modern/useInfiniteQuery.cjs.map +1 -1
- package/build/modern/useInfiniteQuery.d.cts +12 -21
- package/build/modern/useInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useInfiniteQuery.d.ts +12 -21
- package/build/modern/useInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useInfiniteQuery.js.map +1 -1
- package/build/modern/useIsFetching.cjs +0 -3
- package/build/modern/useIsFetching.cjs.map +1 -1
- package/build/modern/useIsFetching.d.cts +0 -3
- package/build/modern/useIsFetching.d.cts.map +1 -1
- package/build/modern/useIsFetching.d.ts +0 -3
- package/build/modern/useIsFetching.d.ts.map +1 -1
- package/build/modern/useIsFetching.js +0 -3
- package/build/modern/useIsFetching.js.map +1 -1
- package/build/modern/useMutation.cjs +4 -8
- package/build/modern/useMutation.cjs.map +1 -1
- package/build/modern/useMutation.d.cts +4 -8
- package/build/modern/useMutation.d.cts.map +1 -1
- package/build/modern/useMutation.d.ts +4 -8
- package/build/modern/useMutation.d.ts.map +1 -1
- package/build/modern/useMutation.js +4 -8
- package/build/modern/useMutation.js.map +1 -1
- package/build/modern/useMutationState.cjs +7 -6
- package/build/modern/useMutationState.cjs.map +1 -1
- package/build/modern/useMutationState.d.cts +0 -6
- package/build/modern/useMutationState.d.cts.map +1 -1
- package/build/modern/useMutationState.d.ts +0 -6
- package/build/modern/useMutationState.d.ts.map +1 -1
- package/build/modern/useMutationState.js +7 -6
- package/build/modern/useMutationState.js.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.cjs +0 -3
- package/build/modern/usePrefetchInfiniteQuery.cjs.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.cts +0 -3
- package/build/modern/usePrefetchInfiniteQuery.d.cts.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.ts +0 -3
- package/build/modern/usePrefetchInfiniteQuery.d.ts.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.js +0 -3
- package/build/modern/usePrefetchInfiniteQuery.js.map +1 -1
- package/build/modern/usePrefetchQuery.cjs +0 -3
- package/build/modern/usePrefetchQuery.cjs.map +1 -1
- package/build/modern/usePrefetchQuery.d.cts +0 -3
- package/build/modern/usePrefetchQuery.d.cts.map +1 -1
- package/build/modern/usePrefetchQuery.d.ts +0 -3
- package/build/modern/usePrefetchQuery.d.ts.map +1 -1
- package/build/modern/usePrefetchQuery.js +0 -3
- package/build/modern/usePrefetchQuery.js.map +1 -1
- package/build/modern/useQueries.cjs +7 -8
- package/build/modern/useQueries.cjs.map +1 -1
- package/build/modern/useQueries.d.cts +7 -11
- package/build/modern/useQueries.d.cts.map +1 -1
- package/build/modern/useQueries.d.ts +7 -11
- package/build/modern/useQueries.d.ts.map +1 -1
- package/build/modern/useQueries.js +7 -8
- package/build/modern/useQueries.js.map +1 -1
- package/build/modern/useQuery.cjs.map +1 -1
- package/build/modern/useQuery.d.cts +8 -13
- package/build/modern/useQuery.d.cts.map +1 -1
- package/build/modern/useQuery.d.ts +8 -13
- package/build/modern/useQuery.d.ts.map +1 -1
- package/build/modern/useQuery.js.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.cjs +6 -8
- package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.cts +6 -8
- package/build/modern/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.ts +6 -8
- package/build/modern/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.js +6 -8
- package/build/modern/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/modern/useSuspenseQueries.cjs.map +1 -1
- package/build/modern/useSuspenseQueries.d.cts +0 -9
- package/build/modern/useSuspenseQueries.d.cts.map +1 -1
- package/build/modern/useSuspenseQueries.d.ts +0 -9
- package/build/modern/useSuspenseQueries.d.ts.map +1 -1
- package/build/modern/useSuspenseQueries.js.map +1 -1
- package/build/modern/useSuspenseQuery.cjs +4 -6
- package/build/modern/useSuspenseQuery.cjs.map +1 -1
- package/build/modern/useSuspenseQuery.d.cts +4 -6
- package/build/modern/useSuspenseQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseQuery.d.ts +4 -6
- package/build/modern/useSuspenseQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseQuery.js +4 -6
- package/build/modern/useSuspenseQuery.js.map +1 -1
- package/package.json +6 -6
- package/src/HydrationBoundary.tsx +4 -5
- package/src/IsRestoringProvider.ts +0 -1
- package/src/QueryClientProvider.tsx +3 -4
- package/src/QueryErrorResetBoundary.tsx +30 -6
- package/src/errorBoundaryUtils.ts +21 -0
- package/src/infiniteQueryOptions.ts +5 -16
- package/src/mutationOptions.ts +2 -6
- package/src/queryOptions.ts +8 -17
- package/src/suspense.ts +29 -1
- package/src/types.ts +2 -22
- package/src/useBaseQuery.ts +13 -0
- package/src/useInfiniteQuery.ts +12 -21
- package/src/useIsFetching.ts +0 -3
- package/src/useMutation.ts +4 -8
- package/src/useMutationState.ts +7 -6
- package/src/usePrefetchInfiniteQuery.tsx +0 -3
- package/src/usePrefetchQuery.tsx +0 -3
- package/src/useQueries.ts +7 -11
- package/src/useQuery.ts +8 -13
- package/src/useSuspenseInfiniteQuery.ts +6 -8
- package/src/useSuspenseQueries.ts +0 -9
- package/src/useSuspenseQuery.ts +4 -6
|
@@ -9,9 +9,6 @@ let _tanstack_query_core = require("@tanstack/query-core");
|
|
|
9
9
|
/**
|
|
10
10
|
* Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
|
|
11
11
|
* `useMutation` is the hook for that.
|
|
12
|
-
*
|
|
13
|
-
* @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look
|
|
14
|
-
* the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
|
|
15
12
|
* @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.
|
|
16
13
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
17
14
|
* be used.
|
|
@@ -20,7 +17,10 @@ let _tanstack_query_core = require("@tanstack/query-core");
|
|
|
20
17
|
* mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call callbacks
|
|
21
18
|
* fire only for the latest call you've made, and only while the component is still mounted — unmounting before
|
|
22
19
|
* the mutation settles removes the subscription and prevents them from firing.
|
|
23
|
-
*
|
|
20
|
+
* @throws {Error} The mutation error, when `throwOnError` is `true` or returns `true` for it, so that it is
|
|
21
|
+
* thrown to the nearest error boundary.
|
|
22
|
+
* @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look
|
|
23
|
+
* the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
|
|
24
24
|
* @example
|
|
25
25
|
* ```tsx
|
|
26
26
|
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
@@ -46,7 +46,6 @@ let _tanstack_query_core = require("@tanstack/query-core");
|
|
|
46
46
|
* )
|
|
47
47
|
* }
|
|
48
48
|
* ```
|
|
49
|
-
*
|
|
50
49
|
* @example
|
|
51
50
|
* Rendering the mutation's own state, rather than just firing it off:
|
|
52
51
|
* ```tsx
|
|
@@ -76,7 +75,6 @@ let _tanstack_query_core = require("@tanstack/query-core");
|
|
|
76
75
|
* )
|
|
77
76
|
* }
|
|
78
77
|
* ```
|
|
79
|
-
*
|
|
80
78
|
* @example
|
|
81
79
|
* Optimistic update via `onMutate`, rolling back on `onError`:
|
|
82
80
|
* ```tsx
|
|
@@ -112,7 +110,6 @@ let _tanstack_query_core = require("@tanstack/query-core");
|
|
|
112
110
|
* )
|
|
113
111
|
* }
|
|
114
112
|
* ```
|
|
115
|
-
*
|
|
116
113
|
* @example
|
|
117
114
|
* Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
|
|
118
115
|
* promise per call instead, so you can wait for all of them when they succeed:
|
|
@@ -142,7 +139,6 @@ let _tanstack_query_core = require("@tanstack/query-core");
|
|
|
142
139
|
* )
|
|
143
140
|
* }
|
|
144
141
|
* ```
|
|
145
|
-
*
|
|
146
142
|
* @example
|
|
147
143
|
* If some of the mutations above can fail independently of the others, and you want to know which ones
|
|
148
144
|
* did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutation.cjs","names":["useQueryClient","React","MutationObserver","notifyManager","noop","shouldThrowError"],"sources":["../../src/useMutation.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\nimport {\n MutationObserver,\n noop,\n notifyManager,\n shouldThrowError,\n} from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n UseMutateFunction,\n UseMutationOptions,\n UseMutationResult,\n} from './types'\nimport type { DefaultError, QueryClient } from '@tanstack/query-core'\n\n// HOOK\n\n/**\n * Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.\n * `useMutation` is the hook for that.\n
|
|
1
|
+
{"version":3,"file":"useMutation.cjs","names":["useQueryClient","React","MutationObserver","notifyManager","noop","shouldThrowError"],"sources":["../../src/useMutation.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\nimport {\n MutationObserver,\n noop,\n notifyManager,\n shouldThrowError,\n} from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n UseMutateFunction,\n UseMutationOptions,\n UseMutationResult,\n} from './types'\nimport type { DefaultError, QueryClient } from '@tanstack/query-core'\n\n// HOOK\n\n/**\n * Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.\n * `useMutation` is the hook for that.\n * @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns `mutate`/`mutateAsync` also accept per-call `onSuccess`/`onError`/`onSettled` callbacks as a second\n * argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared\n * mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call callbacks\n * fire only for the latest call you've made, and only while the component is still mounted — unmounting before\n * the mutation settles removes the subscription and prevents them from firing.\n * @throws {Error} The mutation error, when `throwOnError` is `true` or returns `true` for it, so that it is\n * thrown to the nearest error boundary.\n * @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look\n * the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).\n * @example\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/react-query'\n *\n * function AddTodo() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * return (\n * <button\n * onClick={() =>\n * addMutation.mutate('Item', {\n * onError: (error) => console.error('Failed to add item:', error),\n * })\n * }\n * >\n * Add\n * </button>\n * )\n * }\n * ```\n * @example\n * Rendering the mutation's own state, rather than just firing it off:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/react-query'\n *\n * function AddTodo() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * return (\n * <div>\n * {addMutation.isPending ? (\n * 'Adding todo...'\n * ) : (\n * <>\n * {addMutation.isError ? (\n * <div>An error occurred: {addMutation.error.message}</div>\n * ) : null}\n * <button onClick={() => addMutation.mutate('Item')}>Add</button>\n * </>\n * )}\n * </div>\n * )\n * }\n * ```\n * @example\n * Optimistic update via `onMutate`, rolling back on `onError`:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/react-query'\n *\n * function AddTodo() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onMutate: async (newTodo) => {\n * await queryClient.cancelQueries({ queryKey: ['todos'] })\n * const previousTodos = queryClient.getQueryData<Array<string>>(['todos'])\n *\n * queryClient.setQueryData<Array<string>>(['todos'], (old) => [\n * ...(old ?? []),\n * newTodo,\n * ])\n *\n * // Passed to `onError` as `onMutateResult` if the mutation fails.\n * return { previousTodos }\n * },\n * onError: (_err, _newTodo, onMutateResult) => {\n * queryClient.setQueryData(['todos'], onMutateResult?.previousTodos)\n * },\n * onSettled: () => {\n * queryClient.invalidateQueries({ queryKey: ['todos'] })\n * },\n * })\n *\n * return (\n * <button onClick={() => addMutation.mutate('Item')}>Add</button>\n * )\n * }\n * ```\n * @example\n * Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a\n * promise per call instead, so you can wait for all of them when they succeed:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/react-query'\n *\n * function AddTodos() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * async function handleAddAll(todos: Array<string>) {\n * try {\n * await Promise.all(todos.map((todo) => addMutation.mutateAsync(todo)))\n * } catch (error) {\n * console.error('Failed to add todos:', error)\n * }\n * }\n *\n * return (\n * <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>\n * Add all\n * </button>\n * )\n * }\n * ```\n * @example\n * If some of the mutations above can fail independently of the others, and you want to know which ones\n * did — rather than losing that information the moment the first one rejects — swap `Promise.all` for\n * `Promise.allSettled`:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/react-query'\n *\n * function AddTodos() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * async function handleAddAll(todos: Array<string>) {\n * const addResults = await Promise.allSettled(\n * todos.map((todo) => addMutation.mutateAsync(todo)),\n * )\n *\n * addResults.forEach((addResult, index) => {\n * if (addResult.status === 'rejected') {\n * console.error(`Failed to add \"${todos[index]}\":`, addResult.reason)\n * }\n * })\n * }\n *\n * return (\n * <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>\n * Add all\n * </button>\n * )\n * }\n * ```\n */\nexport function useMutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n>(\n options: UseMutationOptions<TData, TError, TVariables, TOnMutateResult>,\n queryClient?: QueryClient,\n): UseMutationResult<TData, TError, TVariables, TOnMutateResult> {\n const client = useQueryClient(queryClient)\n\n const [observer] = React.useState(\n () =>\n new MutationObserver<TData, TError, TVariables, TOnMutateResult>(\n client,\n options,\n ),\n )\n\n React.useEffect(() => {\n observer.setOptions(options)\n }, [observer, options])\n\n const result = React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n observer.subscribe(notifyManager.batchCalls(onStoreChange)),\n [observer],\n ),\n () => observer.getCurrentResult(),\n () => observer.getCurrentResult(),\n )\n\n const mutate = React.useCallback<\n UseMutateFunction<TData, TError, TVariables, TOnMutateResult>\n >(\n (\n ...args: Parameters<\n UseMutateFunction<TData, TError, TVariables, TOnMutateResult>\n >\n ) => {\n observer.mutate(args[0] as TVariables, args[1]).catch(noop)\n },\n [observer],\n )\n\n if (\n result.error &&\n shouldThrowError(observer.options.throwOnError, [result.error])\n ) {\n throw result.error\n }\n\n return { ...result, mutate, mutateAsync: result.mutate }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0LA,SAAgB,YAMd,SACA,aAC+D;CAC/D,MAAM,SAASA,4BAAAA,eAAe,WAAW;CAEzC,MAAM,CAAC,YAAYC,MAAM,eAErB,IAAIC,qBAAAA,iBACF,QACA,OACF,CACJ;CAEA,MAAM,gBAAgB;EACpB,SAAS,WAAW,OAAO;CAC7B,GAAG,CAAC,UAAU,OAAO,CAAC;CAEtB,MAAM,SAASD,MAAM,qBACnBA,MAAM,aACH,kBACC,SAAS,UAAUE,qBAAAA,cAAc,WAAW,aAAa,CAAC,GAC5D,CAAC,QAAQ,CACX,SACM,SAAS,iBAAiB,SAC1B,SAAS,iBAAiB,CAClC;CAEA,MAAM,SAASF,MAAM,aAIjB,GAAG,SAGA;EACH,SAAS,OAAO,KAAK,IAAkB,KAAK,EAAE,CAAC,CAAC,MAAMG,qBAAAA,IAAI;CAC5D,GACA,CAAC,QAAQ,CACX;CAEA,IACE,OAAO,UAAA,GACPC,qBAAAA,iBAAAA,CAAiB,SAAS,QAAQ,cAAc,CAAC,OAAO,KAAK,CAAC,GAE9D,MAAM,OAAO;CAGf,OAAO;EAAE,GAAG;EAAQ;EAAQ,aAAa,OAAO;CAAO;AACzD"}
|
|
@@ -4,9 +4,6 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
|
4
4
|
/**
|
|
5
5
|
* Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
|
|
6
6
|
* `useMutation` is the hook for that.
|
|
7
|
-
*
|
|
8
|
-
* @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look
|
|
9
|
-
* the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
|
|
10
7
|
* @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.
|
|
11
8
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
12
9
|
* be used.
|
|
@@ -15,7 +12,10 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
|
15
12
|
* mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call callbacks
|
|
16
13
|
* fire only for the latest call you've made, and only while the component is still mounted — unmounting before
|
|
17
14
|
* the mutation settles removes the subscription and prevents them from firing.
|
|
18
|
-
*
|
|
15
|
+
* @throws {Error} The mutation error, when `throwOnError` is `true` or returns `true` for it, so that it is
|
|
16
|
+
* thrown to the nearest error boundary.
|
|
17
|
+
* @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look
|
|
18
|
+
* the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
|
|
19
19
|
* @example
|
|
20
20
|
* ```tsx
|
|
21
21
|
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
@@ -41,7 +41,6 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
|
41
41
|
* )
|
|
42
42
|
* }
|
|
43
43
|
* ```
|
|
44
|
-
*
|
|
45
44
|
* @example
|
|
46
45
|
* Rendering the mutation's own state, rather than just firing it off:
|
|
47
46
|
* ```tsx
|
|
@@ -71,7 +70,6 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
|
71
70
|
* )
|
|
72
71
|
* }
|
|
73
72
|
* ```
|
|
74
|
-
*
|
|
75
73
|
* @example
|
|
76
74
|
* Optimistic update via `onMutate`, rolling back on `onError`:
|
|
77
75
|
* ```tsx
|
|
@@ -107,7 +105,6 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
|
107
105
|
* )
|
|
108
106
|
* }
|
|
109
107
|
* ```
|
|
110
|
-
*
|
|
111
108
|
* @example
|
|
112
109
|
* Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
|
|
113
110
|
* promise per call instead, so you can wait for all of them when they succeed:
|
|
@@ -137,7 +134,6 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
|
137
134
|
* )
|
|
138
135
|
* }
|
|
139
136
|
* ```
|
|
140
|
-
*
|
|
141
137
|
* @example
|
|
142
138
|
* If some of the mutations above can fail independently of the others, and you want to know which ones
|
|
143
139
|
* did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutation.d.cts","names":[],"sources":["../../src/useMutation.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useMutation.d.cts","names":[],"sources":["../../src/useMutation.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA0LgB,YACd,iBACA,SAAS,cACT,mBACA,2BAEA,SAAS,mBAAmB,OAAO,QAAQ,YAAY,kBACvD,cAAc,cACb,kBAAkB,OAAO,QAAQ,YAAY"}
|
|
@@ -4,9 +4,6 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
|
4
4
|
/**
|
|
5
5
|
* Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
|
|
6
6
|
* `useMutation` is the hook for that.
|
|
7
|
-
*
|
|
8
|
-
* @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look
|
|
9
|
-
* the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
|
|
10
7
|
* @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.
|
|
11
8
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
12
9
|
* be used.
|
|
@@ -15,7 +12,10 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
|
15
12
|
* mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call callbacks
|
|
16
13
|
* fire only for the latest call you've made, and only while the component is still mounted — unmounting before
|
|
17
14
|
* the mutation settles removes the subscription and prevents them from firing.
|
|
18
|
-
*
|
|
15
|
+
* @throws {Error} The mutation error, when `throwOnError` is `true` or returns `true` for it, so that it is
|
|
16
|
+
* thrown to the nearest error boundary.
|
|
17
|
+
* @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look
|
|
18
|
+
* the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
|
|
19
19
|
* @example
|
|
20
20
|
* ```tsx
|
|
21
21
|
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
@@ -41,7 +41,6 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
|
41
41
|
* )
|
|
42
42
|
* }
|
|
43
43
|
* ```
|
|
44
|
-
*
|
|
45
44
|
* @example
|
|
46
45
|
* Rendering the mutation's own state, rather than just firing it off:
|
|
47
46
|
* ```tsx
|
|
@@ -71,7 +70,6 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
|
71
70
|
* )
|
|
72
71
|
* }
|
|
73
72
|
* ```
|
|
74
|
-
*
|
|
75
73
|
* @example
|
|
76
74
|
* Optimistic update via `onMutate`, rolling back on `onError`:
|
|
77
75
|
* ```tsx
|
|
@@ -107,7 +105,6 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
|
107
105
|
* )
|
|
108
106
|
* }
|
|
109
107
|
* ```
|
|
110
|
-
*
|
|
111
108
|
* @example
|
|
112
109
|
* Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
|
|
113
110
|
* promise per call instead, so you can wait for all of them when they succeed:
|
|
@@ -137,7 +134,6 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
|
137
134
|
* )
|
|
138
135
|
* }
|
|
139
136
|
* ```
|
|
140
|
-
*
|
|
141
137
|
* @example
|
|
142
138
|
* If some of the mutations above can fail independently of the others, and you want to know which ones
|
|
143
139
|
* did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutation.d.ts","names":[],"sources":["../../src/useMutation.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useMutation.d.ts","names":[],"sources":["../../src/useMutation.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA0LgB,YACd,iBACA,SAAS,cACT,mBACA,2BAEA,SAAS,mBAAmB,OAAO,QAAQ,YAAY,kBACvD,cAAc,cACb,kBAAkB,OAAO,QAAQ,YAAY"}
|
|
@@ -6,9 +6,6 @@ import { MutationObserver, noop, notifyManager, shouldThrowError } from "@tansta
|
|
|
6
6
|
/**
|
|
7
7
|
* Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
|
|
8
8
|
* `useMutation` is the hook for that.
|
|
9
|
-
*
|
|
10
|
-
* @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look
|
|
11
|
-
* the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
|
|
12
9
|
* @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.
|
|
13
10
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
14
11
|
* be used.
|
|
@@ -17,7 +14,10 @@ import { MutationObserver, noop, notifyManager, shouldThrowError } from "@tansta
|
|
|
17
14
|
* mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call callbacks
|
|
18
15
|
* fire only for the latest call you've made, and only while the component is still mounted — unmounting before
|
|
19
16
|
* the mutation settles removes the subscription and prevents them from firing.
|
|
20
|
-
*
|
|
17
|
+
* @throws {Error} The mutation error, when `throwOnError` is `true` or returns `true` for it, so that it is
|
|
18
|
+
* thrown to the nearest error boundary.
|
|
19
|
+
* @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look
|
|
20
|
+
* the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
|
|
21
21
|
* @example
|
|
22
22
|
* ```tsx
|
|
23
23
|
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
@@ -43,7 +43,6 @@ import { MutationObserver, noop, notifyManager, shouldThrowError } from "@tansta
|
|
|
43
43
|
* )
|
|
44
44
|
* }
|
|
45
45
|
* ```
|
|
46
|
-
*
|
|
47
46
|
* @example
|
|
48
47
|
* Rendering the mutation's own state, rather than just firing it off:
|
|
49
48
|
* ```tsx
|
|
@@ -73,7 +72,6 @@ import { MutationObserver, noop, notifyManager, shouldThrowError } from "@tansta
|
|
|
73
72
|
* )
|
|
74
73
|
* }
|
|
75
74
|
* ```
|
|
76
|
-
*
|
|
77
75
|
* @example
|
|
78
76
|
* Optimistic update via `onMutate`, rolling back on `onError`:
|
|
79
77
|
* ```tsx
|
|
@@ -109,7 +107,6 @@ import { MutationObserver, noop, notifyManager, shouldThrowError } from "@tansta
|
|
|
109
107
|
* )
|
|
110
108
|
* }
|
|
111
109
|
* ```
|
|
112
|
-
*
|
|
113
110
|
* @example
|
|
114
111
|
* Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
|
|
115
112
|
* promise per call instead, so you can wait for all of them when they succeed:
|
|
@@ -139,7 +136,6 @@ import { MutationObserver, noop, notifyManager, shouldThrowError } from "@tansta
|
|
|
139
136
|
* )
|
|
140
137
|
* }
|
|
141
138
|
* ```
|
|
142
|
-
*
|
|
143
139
|
* @example
|
|
144
140
|
* If some of the mutations above can fail independently of the others, and you want to know which ones
|
|
145
141
|
* did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutation.js","names":[],"sources":["../../src/useMutation.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\nimport {\n MutationObserver,\n noop,\n notifyManager,\n shouldThrowError,\n} from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n UseMutateFunction,\n UseMutationOptions,\n UseMutationResult,\n} from './types'\nimport type { DefaultError, QueryClient } from '@tanstack/query-core'\n\n// HOOK\n\n/**\n * Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.\n * `useMutation` is the hook for that.\n
|
|
1
|
+
{"version":3,"file":"useMutation.js","names":[],"sources":["../../src/useMutation.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\nimport {\n MutationObserver,\n noop,\n notifyManager,\n shouldThrowError,\n} from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n UseMutateFunction,\n UseMutationOptions,\n UseMutationResult,\n} from './types'\nimport type { DefaultError, QueryClient } from '@tanstack/query-core'\n\n// HOOK\n\n/**\n * Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.\n * `useMutation` is the hook for that.\n * @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns `mutate`/`mutateAsync` also accept per-call `onSuccess`/`onError`/`onSettled` callbacks as a second\n * argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared\n * mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call callbacks\n * fire only for the latest call you've made, and only while the component is still mounted — unmounting before\n * the mutation settles removes the subscription and prevents them from firing.\n * @throws {Error} The mutation error, when `throwOnError` is `true` or returns `true` for it, so that it is\n * thrown to the nearest error boundary.\n * @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look\n * the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).\n * @example\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/react-query'\n *\n * function AddTodo() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * return (\n * <button\n * onClick={() =>\n * addMutation.mutate('Item', {\n * onError: (error) => console.error('Failed to add item:', error),\n * })\n * }\n * >\n * Add\n * </button>\n * )\n * }\n * ```\n * @example\n * Rendering the mutation's own state, rather than just firing it off:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/react-query'\n *\n * function AddTodo() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * return (\n * <div>\n * {addMutation.isPending ? (\n * 'Adding todo...'\n * ) : (\n * <>\n * {addMutation.isError ? (\n * <div>An error occurred: {addMutation.error.message}</div>\n * ) : null}\n * <button onClick={() => addMutation.mutate('Item')}>Add</button>\n * </>\n * )}\n * </div>\n * )\n * }\n * ```\n * @example\n * Optimistic update via `onMutate`, rolling back on `onError`:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/react-query'\n *\n * function AddTodo() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onMutate: async (newTodo) => {\n * await queryClient.cancelQueries({ queryKey: ['todos'] })\n * const previousTodos = queryClient.getQueryData<Array<string>>(['todos'])\n *\n * queryClient.setQueryData<Array<string>>(['todos'], (old) => [\n * ...(old ?? []),\n * newTodo,\n * ])\n *\n * // Passed to `onError` as `onMutateResult` if the mutation fails.\n * return { previousTodos }\n * },\n * onError: (_err, _newTodo, onMutateResult) => {\n * queryClient.setQueryData(['todos'], onMutateResult?.previousTodos)\n * },\n * onSettled: () => {\n * queryClient.invalidateQueries({ queryKey: ['todos'] })\n * },\n * })\n *\n * return (\n * <button onClick={() => addMutation.mutate('Item')}>Add</button>\n * )\n * }\n * ```\n * @example\n * Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a\n * promise per call instead, so you can wait for all of them when they succeed:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/react-query'\n *\n * function AddTodos() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * async function handleAddAll(todos: Array<string>) {\n * try {\n * await Promise.all(todos.map((todo) => addMutation.mutateAsync(todo)))\n * } catch (error) {\n * console.error('Failed to add todos:', error)\n * }\n * }\n *\n * return (\n * <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>\n * Add all\n * </button>\n * )\n * }\n * ```\n * @example\n * If some of the mutations above can fail independently of the others, and you want to know which ones\n * did — rather than losing that information the moment the first one rejects — swap `Promise.all` for\n * `Promise.allSettled`:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/react-query'\n *\n * function AddTodos() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * async function handleAddAll(todos: Array<string>) {\n * const addResults = await Promise.allSettled(\n * todos.map((todo) => addMutation.mutateAsync(todo)),\n * )\n *\n * addResults.forEach((addResult, index) => {\n * if (addResult.status === 'rejected') {\n * console.error(`Failed to add \"${todos[index]}\":`, addResult.reason)\n * }\n * })\n * }\n *\n * return (\n * <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>\n * Add all\n * </button>\n * )\n * }\n * ```\n */\nexport function useMutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n>(\n options: UseMutationOptions<TData, TError, TVariables, TOnMutateResult>,\n queryClient?: QueryClient,\n): UseMutationResult<TData, TError, TVariables, TOnMutateResult> {\n const client = useQueryClient(queryClient)\n\n const [observer] = React.useState(\n () =>\n new MutationObserver<TData, TError, TVariables, TOnMutateResult>(\n client,\n options,\n ),\n )\n\n React.useEffect(() => {\n observer.setOptions(options)\n }, [observer, options])\n\n const result = React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n observer.subscribe(notifyManager.batchCalls(onStoreChange)),\n [observer],\n ),\n () => observer.getCurrentResult(),\n () => observer.getCurrentResult(),\n )\n\n const mutate = React.useCallback<\n UseMutateFunction<TData, TError, TVariables, TOnMutateResult>\n >(\n (\n ...args: Parameters<\n UseMutateFunction<TData, TError, TVariables, TOnMutateResult>\n >\n ) => {\n observer.mutate(args[0] as TVariables, args[1]).catch(noop)\n },\n [observer],\n )\n\n if (\n result.error &&\n shouldThrowError(observer.options.throwOnError, [result.error])\n ) {\n throw result.error\n }\n\n return { ...result, mutate, mutateAsync: result.mutate }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0LA,SAAgB,YAMd,SACA,aAC+D;CAC/D,MAAM,SAAS,eAAe,WAAW;CAEzC,MAAM,CAAC,YAAY,MAAM,eAErB,IAAI,iBACF,QACA,OACF,CACJ;CAEA,MAAM,gBAAgB;EACpB,SAAS,WAAW,OAAO;CAC7B,GAAG,CAAC,UAAU,OAAO,CAAC;CAEtB,MAAM,SAAS,MAAM,qBACnB,MAAM,aACH,kBACC,SAAS,UAAU,cAAc,WAAW,aAAa,CAAC,GAC5D,CAAC,QAAQ,CACX,SACM,SAAS,iBAAiB,SAC1B,SAAS,iBAAiB,CAClC;CAEA,MAAM,SAAS,MAAM,aAIjB,GAAG,SAGA;EACH,SAAS,OAAO,KAAK,IAAkB,KAAK,EAAE,CAAC,CAAC,MAAM,IAAI;CAC5D,GACA,CAAC,QAAQ,CACX;CAEA,IACE,OAAO,SACP,iBAAiB,SAAS,QAAQ,cAAc,CAAC,OAAO,KAAK,CAAC,GAE9D,MAAM,OAAO;CAGf,OAAO;EAAE,GAAG;EAAQ;EAAQ,aAAa,OAAO;CAAO;AACzD"}
|
|
@@ -9,12 +9,10 @@ let _tanstack_query_core = require("@tanstack/query-core");
|
|
|
9
9
|
/**
|
|
10
10
|
* The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`
|
|
11
11
|
* (useful for app-wide loading indicators).
|
|
12
|
-
*
|
|
13
12
|
* @param filters - The {@link MutationFilters} to narrow down the matched mutations.
|
|
14
13
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
15
14
|
* be used.
|
|
16
15
|
* @returns Will be the `number` of the mutations that your application currently has `pending`.
|
|
17
|
-
*
|
|
18
16
|
* @example
|
|
19
17
|
* ```tsx
|
|
20
18
|
* import { useIsMutating } from '@tanstack/react-query'
|
|
@@ -34,6 +32,13 @@ function useIsMutating(filters, queryClient) {
|
|
|
34
32
|
status: "pending"
|
|
35
33
|
} }, client).length;
|
|
36
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Collects the mutations in the cache that match `options.filters`, mapped with `options.select`
|
|
37
|
+
* (or to their state, by default).
|
|
38
|
+
* @param mutationCache - The mutation cache to read.
|
|
39
|
+
* @param options - The `filters` to match and the `select` function to map each mutation with.
|
|
40
|
+
* @returns The selected value of every matching mutation.
|
|
41
|
+
*/
|
|
37
42
|
function getResult(mutationCache, options) {
|
|
38
43
|
return mutationCache.findAll(options.filters).map((mutation) => options.select ? options.select(mutation) : mutation.state);
|
|
39
44
|
}
|
|
@@ -41,13 +46,11 @@ function getResult(mutationCache, options) {
|
|
|
41
46
|
* `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass
|
|
42
47
|
* `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
|
|
43
48
|
* state.
|
|
44
|
-
*
|
|
45
49
|
* @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
|
|
46
50
|
* mutation state.
|
|
47
51
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
48
52
|
* be used.
|
|
49
53
|
* @returns Will be an Array of whatever `select` returns for each matching mutation.
|
|
50
|
-
*
|
|
51
54
|
* @example
|
|
52
55
|
* Get all variables of all running mutations:
|
|
53
56
|
* ```tsx
|
|
@@ -62,7 +65,6 @@ function getResult(mutationCache, options) {
|
|
|
62
65
|
* return <>{pendingVariables.length} posts saving...</>
|
|
63
66
|
* }
|
|
64
67
|
* ```
|
|
65
|
-
*
|
|
66
68
|
* @example
|
|
67
69
|
* Get all data for specific mutations via the `mutationKey`:
|
|
68
70
|
* ```tsx
|
|
@@ -90,7 +92,6 @@ function getResult(mutationCache, options) {
|
|
|
90
92
|
* )
|
|
91
93
|
* }
|
|
92
94
|
* ```
|
|
93
|
-
*
|
|
94
95
|
* @example
|
|
95
96
|
* Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new
|
|
96
97
|
* entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.cjs","names":["useQueryClient","React","replaceEqualDeep"],"sources":["../../src/useMutationState.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\n\nimport { notifyManager, replaceEqualDeep } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n QueryClient,\n} from '@tanstack/query-core'\n\n/**\n * The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`\n * (useful for app-wide loading indicators).\n
|
|
1
|
+
{"version":3,"file":"useMutationState.cjs","names":["useQueryClient","React","replaceEqualDeep"],"sources":["../../src/useMutationState.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\n\nimport { notifyManager, replaceEqualDeep } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n QueryClient,\n} from '@tanstack/query-core'\n\n/**\n * The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`\n * (useful for app-wide loading indicators).\n * @param filters - The {@link MutationFilters} to narrow down the matched mutations.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be the `number` of the mutations that your application currently has `pending`.\n * @example\n * ```tsx\n * import { useIsMutating } from '@tanstack/react-query'\n *\n * function PostsMutatingIndicator() {\n * // How many mutations matching the posts prefix are in progress?\n * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })\n *\n * return isMutatingPosts ? <span>Saving posts...</span> : null\n * }\n * ```\n */\nexport function useIsMutating(\n filters?: MutationFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n return useMutationState(\n { filters: { ...filters, status: 'pending' } },\n client,\n ).length\n}\n\ntype MutationTypeFromResult<TResult> = [TResult] extends [\n MutationState<\n infer TData,\n infer TError,\n infer TVariables,\n infer TOnMutateResult\n >,\n]\n ? Mutation<TData, TError, TVariables, TOnMutateResult>\n : Mutation\n\ntype MutationStateOptions<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n> = {\n filters?: MutationFilters\n select?: (mutation: TMutation) => TResult\n}\n\n/**\n * Collects the mutations in the cache that match `options.filters`, mapped with `options.select`\n * (or to their state, by default).\n * @param mutationCache - The mutation cache to read.\n * @param options - The `filters` to match and the `select` function to map each mutation with.\n * @returns The selected value of every matching mutation.\n */\nfunction getResult<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n mutationCache: MutationCache,\n options: MutationStateOptions<TResult, TMutation>,\n): Array<TResult> {\n return mutationCache\n .findAll(options.filters)\n .map(\n (mutation): TResult =>\n (options.select\n ? options.select(mutation as TMutation)\n : mutation.state) as TResult,\n )\n}\n\n/**\n * `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass\n * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation\n * state.\n * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the\n * mutation state.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be an Array of whatever `select` returns for each matching mutation.\n * @example\n * Get all variables of all running mutations:\n * ```tsx\n * import { useMutationState } from '@tanstack/react-query'\n *\n * function PendingPosts() {\n * const pendingVariables = useMutationState({\n * filters: { status: 'pending' },\n * select: (mutation) => mutation.state.variables,\n * })\n *\n * return <>{pendingVariables.length} posts saving...</>\n * }\n * ```\n * @example\n * Get all data for specific mutations via the `mutationKey`:\n * ```tsx\n * import { useMutation, useMutationState } from '@tanstack/react-query'\n *\n * const mutationKey = ['posts']\n *\n * function Posts() {\n * // Some mutation that we want to get the state for\n * const mutation = useMutation({\n * mutationKey,\n * mutationFn: createPosts,\n * })\n *\n * const savedPosts = useMutationState({\n * // this mutation key needs to match the mutation key of the given mutation (see above)\n * filters: { mutationKey, status: 'success' },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * return (\n * <button onClick={() => mutation.mutate(['New Post'])}>\n * Create post ({savedPosts.length} saved so far)\n * </button>\n * )\n * }\n * ```\n * @example\n * Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new\n * entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the\n * last item that `useMutationState` returns to get the latest successful invocation:\n * ```tsx\n * import { useMutationState } from '@tanstack/react-query'\n *\n * function LatestPost() {\n * const savedPosts = useMutationState({\n * filters: { mutationKey: ['posts'], status: 'success' },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * const latestSavedPost = savedPosts[savedPosts.length - 1]\n *\n * return <>{latestSavedPost ? 'Saved' : 'Nothing saved yet'}</>\n * }\n * ```\n */\nexport function useMutationState<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n options: MutationStateOptions<TResult, TMutation> = {},\n queryClient?: QueryClient,\n): Array<TResult> {\n const mutationCache = useQueryClient(queryClient).getMutationCache()\n const optionsRef = React.useRef(options)\n const resultRef = React.useRef<Array<TResult>>(null)\n if (resultRef.current === null) {\n resultRef.current = getResult(mutationCache, options)\n }\n\n React.useEffect(() => {\n optionsRef.current = options\n })\n\n return React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n mutationCache.subscribe(() => {\n const nextResult = replaceEqualDeep(\n resultRef.current,\n getResult(mutationCache, optionsRef.current),\n )\n if (resultRef.current !== nextResult) {\n resultRef.current = nextResult\n notifyManager.schedule(onStoreChange)\n }\n }),\n [mutationCache],\n ),\n () => resultRef.current,\n () => resultRef.current,\n )!\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,cACd,SACA,aACQ;CACR,MAAM,SAASA,4BAAAA,eAAe,WAAW;CACzC,OAAO,iBACL,EAAE,SAAS;EAAE,GAAG;EAAS,QAAQ;CAAU,EAAE,GAC7C,MACF,CAAC,CAAC;AACJ;;;;;;;;AA6BA,SAAS,UAKP,eACA,SACgB;CAChB,OAAO,cACJ,QAAQ,QAAQ,OAAO,CAAC,CACxB,KACE,aACE,QAAQ,SACL,QAAQ,OAAO,QAAqB,IACpC,SAAS,KACjB;AACJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,SAAgB,iBAKd,UAAoD,CAAC,GACrD,aACgB;CAChB,MAAM,gBAAgBA,4BAAAA,eAAe,WAAW,CAAC,CAAC,iBAAiB;CACnE,MAAM,aAAaC,MAAM,OAAO,OAAO;CACvC,MAAM,YAAYA,MAAM,OAAuB,IAAI;CACnD,IAAI,UAAU,YAAY,MACxB,UAAU,UAAU,UAAU,eAAe,OAAO;CAGtD,MAAM,gBAAgB;EACpB,WAAW,UAAU;CACvB,CAAC;CAED,OAAOA,MAAM,qBACXA,MAAM,aACH,kBACC,cAAc,gBAAgB;EAC5B,MAAM,cAAA,GAAaC,qBAAAA,iBAAAA,CACjB,UAAU,SACV,UAAU,eAAe,WAAW,OAAO,CAC7C;EACA,IAAI,UAAU,YAAY,YAAY;GACpC,UAAU,UAAU;GACpB,qBAAA,cAAc,SAAS,aAAa;EACtC;CACF,CAAC,GACH,CAAC,aAAa,CAChB,SACM,UAAU,eACV,UAAU,OAClB;AACF"}
|
|
@@ -3,12 +3,10 @@ import { Mutation, MutationFilters, MutationState, QueryClient } from "@tanstack
|
|
|
3
3
|
/**
|
|
4
4
|
* The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`
|
|
5
5
|
* (useful for app-wide loading indicators).
|
|
6
|
-
*
|
|
7
6
|
* @param filters - The {@link MutationFilters} to narrow down the matched mutations.
|
|
8
7
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
9
8
|
* be used.
|
|
10
9
|
* @returns Will be the `number` of the mutations that your application currently has `pending`.
|
|
11
|
-
*
|
|
12
10
|
* @example
|
|
13
11
|
* ```tsx
|
|
14
12
|
* import { useIsMutating } from '@tanstack/react-query'
|
|
@@ -31,13 +29,11 @@ type MutationStateOptions<TResult = MutationState, TMutation extends Mutation<an
|
|
|
31
29
|
* `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass
|
|
32
30
|
* `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
|
|
33
31
|
* state.
|
|
34
|
-
*
|
|
35
32
|
* @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
|
|
36
33
|
* mutation state.
|
|
37
34
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
38
35
|
* be used.
|
|
39
36
|
* @returns Will be an Array of whatever `select` returns for each matching mutation.
|
|
40
|
-
*
|
|
41
37
|
* @example
|
|
42
38
|
* Get all variables of all running mutations:
|
|
43
39
|
* ```tsx
|
|
@@ -52,7 +48,6 @@ type MutationStateOptions<TResult = MutationState, TMutation extends Mutation<an
|
|
|
52
48
|
* return <>{pendingVariables.length} posts saving...</>
|
|
53
49
|
* }
|
|
54
50
|
* ```
|
|
55
|
-
*
|
|
56
51
|
* @example
|
|
57
52
|
* Get all data for specific mutations via the `mutationKey`:
|
|
58
53
|
* ```tsx
|
|
@@ -80,7 +75,6 @@ type MutationStateOptions<TResult = MutationState, TMutation extends Mutation<an
|
|
|
80
75
|
* )
|
|
81
76
|
* }
|
|
82
77
|
* ```
|
|
83
|
-
*
|
|
84
78
|
* @example
|
|
85
79
|
* Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new
|
|
86
80
|
* entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.d.cts","names":[],"sources":["../../src/useMutationState.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useMutationState.d.cts","names":[],"sources":["../../src/useMutationState.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAgCgB,cACd,UAAU,iBACV,cAAc;KASX,uBAAuB,YAAY,kBACtC,oBACQ,aACA,cACA,kBACA,oBAGN,SAAS,OAAO,QAAQ,YAAY,mBACpC;KAEC,qBACH,UAAU,eACV,kBAAkB,+BAChB,uBAAuB;EAEzB,UAAU;EACV,UAAU,UAAU,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAiGpB,iBACd,UAAU,eACV,kBAAkB,+BAChB,uBAAuB,UAEzB,UAAS,qBAAqB,SAAS,YACvC,cAAc,cACb,MAAM"}
|
|
@@ -3,12 +3,10 @@ import { Mutation, MutationFilters, MutationState, QueryClient } from "@tanstack
|
|
|
3
3
|
/**
|
|
4
4
|
* The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`
|
|
5
5
|
* (useful for app-wide loading indicators).
|
|
6
|
-
*
|
|
7
6
|
* @param filters - The {@link MutationFilters} to narrow down the matched mutations.
|
|
8
7
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
9
8
|
* be used.
|
|
10
9
|
* @returns Will be the `number` of the mutations that your application currently has `pending`.
|
|
11
|
-
*
|
|
12
10
|
* @example
|
|
13
11
|
* ```tsx
|
|
14
12
|
* import { useIsMutating } from '@tanstack/react-query'
|
|
@@ -31,13 +29,11 @@ type MutationStateOptions<TResult = MutationState, TMutation extends Mutation<an
|
|
|
31
29
|
* `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass
|
|
32
30
|
* `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
|
|
33
31
|
* state.
|
|
34
|
-
*
|
|
35
32
|
* @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
|
|
36
33
|
* mutation state.
|
|
37
34
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
38
35
|
* be used.
|
|
39
36
|
* @returns Will be an Array of whatever `select` returns for each matching mutation.
|
|
40
|
-
*
|
|
41
37
|
* @example
|
|
42
38
|
* Get all variables of all running mutations:
|
|
43
39
|
* ```tsx
|
|
@@ -52,7 +48,6 @@ type MutationStateOptions<TResult = MutationState, TMutation extends Mutation<an
|
|
|
52
48
|
* return <>{pendingVariables.length} posts saving...</>
|
|
53
49
|
* }
|
|
54
50
|
* ```
|
|
55
|
-
*
|
|
56
51
|
* @example
|
|
57
52
|
* Get all data for specific mutations via the `mutationKey`:
|
|
58
53
|
* ```tsx
|
|
@@ -80,7 +75,6 @@ type MutationStateOptions<TResult = MutationState, TMutation extends Mutation<an
|
|
|
80
75
|
* )
|
|
81
76
|
* }
|
|
82
77
|
* ```
|
|
83
|
-
*
|
|
84
78
|
* @example
|
|
85
79
|
* Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new
|
|
86
80
|
* entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.d.ts","names":[],"sources":["../../src/useMutationState.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useMutationState.d.ts","names":[],"sources":["../../src/useMutationState.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAgCgB,cACd,UAAU,iBACV,cAAc;KASX,uBAAuB,YAAY,kBACtC,oBACQ,aACA,cACA,kBACA,oBAGN,SAAS,OAAO,QAAQ,YAAY,mBACpC;KAEC,qBACH,UAAU,eACV,kBAAkB,+BAChB,uBAAuB;EAEzB,UAAU;EACV,UAAU,UAAU,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAiGpB,iBACd,UAAU,eACV,kBAAkB,+BAChB,uBAAuB,UAEzB,UAAS,qBAAqB,SAAS,YACvC,cAAc,cACb,MAAM"}
|
|
@@ -6,12 +6,10 @@ import { notifyManager, replaceEqualDeep } from "@tanstack/query-core";
|
|
|
6
6
|
/**
|
|
7
7
|
* The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`
|
|
8
8
|
* (useful for app-wide loading indicators).
|
|
9
|
-
*
|
|
10
9
|
* @param filters - The {@link MutationFilters} to narrow down the matched mutations.
|
|
11
10
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
12
11
|
* be used.
|
|
13
12
|
* @returns Will be the `number` of the mutations that your application currently has `pending`.
|
|
14
|
-
*
|
|
15
13
|
* @example
|
|
16
14
|
* ```tsx
|
|
17
15
|
* import { useIsMutating } from '@tanstack/react-query'
|
|
@@ -31,6 +29,13 @@ function useIsMutating(filters, queryClient) {
|
|
|
31
29
|
status: "pending"
|
|
32
30
|
} }, client).length;
|
|
33
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Collects the mutations in the cache that match `options.filters`, mapped with `options.select`
|
|
34
|
+
* (or to their state, by default).
|
|
35
|
+
* @param mutationCache - The mutation cache to read.
|
|
36
|
+
* @param options - The `filters` to match and the `select` function to map each mutation with.
|
|
37
|
+
* @returns The selected value of every matching mutation.
|
|
38
|
+
*/
|
|
34
39
|
function getResult(mutationCache, options) {
|
|
35
40
|
return mutationCache.findAll(options.filters).map((mutation) => options.select ? options.select(mutation) : mutation.state);
|
|
36
41
|
}
|
|
@@ -38,13 +43,11 @@ function getResult(mutationCache, options) {
|
|
|
38
43
|
* `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass
|
|
39
44
|
* `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
|
|
40
45
|
* state.
|
|
41
|
-
*
|
|
42
46
|
* @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
|
|
43
47
|
* mutation state.
|
|
44
48
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
45
49
|
* be used.
|
|
46
50
|
* @returns Will be an Array of whatever `select` returns for each matching mutation.
|
|
47
|
-
*
|
|
48
51
|
* @example
|
|
49
52
|
* Get all variables of all running mutations:
|
|
50
53
|
* ```tsx
|
|
@@ -59,7 +62,6 @@ function getResult(mutationCache, options) {
|
|
|
59
62
|
* return <>{pendingVariables.length} posts saving...</>
|
|
60
63
|
* }
|
|
61
64
|
* ```
|
|
62
|
-
*
|
|
63
65
|
* @example
|
|
64
66
|
* Get all data for specific mutations via the `mutationKey`:
|
|
65
67
|
* ```tsx
|
|
@@ -87,7 +89,6 @@ function getResult(mutationCache, options) {
|
|
|
87
89
|
* )
|
|
88
90
|
* }
|
|
89
91
|
* ```
|
|
90
|
-
*
|
|
91
92
|
* @example
|
|
92
93
|
* Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new
|
|
93
94
|
* entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.js","names":[],"sources":["../../src/useMutationState.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\n\nimport { notifyManager, replaceEqualDeep } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n QueryClient,\n} from '@tanstack/query-core'\n\n/**\n * The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`\n * (useful for app-wide loading indicators).\n
|
|
1
|
+
{"version":3,"file":"useMutationState.js","names":[],"sources":["../../src/useMutationState.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\n\nimport { notifyManager, replaceEqualDeep } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n QueryClient,\n} from '@tanstack/query-core'\n\n/**\n * The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`\n * (useful for app-wide loading indicators).\n * @param filters - The {@link MutationFilters} to narrow down the matched mutations.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be the `number` of the mutations that your application currently has `pending`.\n * @example\n * ```tsx\n * import { useIsMutating } from '@tanstack/react-query'\n *\n * function PostsMutatingIndicator() {\n * // How many mutations matching the posts prefix are in progress?\n * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })\n *\n * return isMutatingPosts ? <span>Saving posts...</span> : null\n * }\n * ```\n */\nexport function useIsMutating(\n filters?: MutationFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n return useMutationState(\n { filters: { ...filters, status: 'pending' } },\n client,\n ).length\n}\n\ntype MutationTypeFromResult<TResult> = [TResult] extends [\n MutationState<\n infer TData,\n infer TError,\n infer TVariables,\n infer TOnMutateResult\n >,\n]\n ? Mutation<TData, TError, TVariables, TOnMutateResult>\n : Mutation\n\ntype MutationStateOptions<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n> = {\n filters?: MutationFilters\n select?: (mutation: TMutation) => TResult\n}\n\n/**\n * Collects the mutations in the cache that match `options.filters`, mapped with `options.select`\n * (or to their state, by default).\n * @param mutationCache - The mutation cache to read.\n * @param options - The `filters` to match and the `select` function to map each mutation with.\n * @returns The selected value of every matching mutation.\n */\nfunction getResult<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n mutationCache: MutationCache,\n options: MutationStateOptions<TResult, TMutation>,\n): Array<TResult> {\n return mutationCache\n .findAll(options.filters)\n .map(\n (mutation): TResult =>\n (options.select\n ? options.select(mutation as TMutation)\n : mutation.state) as TResult,\n )\n}\n\n/**\n * `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass\n * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation\n * state.\n * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the\n * mutation state.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be an Array of whatever `select` returns for each matching mutation.\n * @example\n * Get all variables of all running mutations:\n * ```tsx\n * import { useMutationState } from '@tanstack/react-query'\n *\n * function PendingPosts() {\n * const pendingVariables = useMutationState({\n * filters: { status: 'pending' },\n * select: (mutation) => mutation.state.variables,\n * })\n *\n * return <>{pendingVariables.length} posts saving...</>\n * }\n * ```\n * @example\n * Get all data for specific mutations via the `mutationKey`:\n * ```tsx\n * import { useMutation, useMutationState } from '@tanstack/react-query'\n *\n * const mutationKey = ['posts']\n *\n * function Posts() {\n * // Some mutation that we want to get the state for\n * const mutation = useMutation({\n * mutationKey,\n * mutationFn: createPosts,\n * })\n *\n * const savedPosts = useMutationState({\n * // this mutation key needs to match the mutation key of the given mutation (see above)\n * filters: { mutationKey, status: 'success' },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * return (\n * <button onClick={() => mutation.mutate(['New Post'])}>\n * Create post ({savedPosts.length} saved so far)\n * </button>\n * )\n * }\n * ```\n * @example\n * Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new\n * entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the\n * last item that `useMutationState` returns to get the latest successful invocation:\n * ```tsx\n * import { useMutationState } from '@tanstack/react-query'\n *\n * function LatestPost() {\n * const savedPosts = useMutationState({\n * filters: { mutationKey: ['posts'], status: 'success' },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * const latestSavedPost = savedPosts[savedPosts.length - 1]\n *\n * return <>{latestSavedPost ? 'Saved' : 'Nothing saved yet'}</>\n * }\n * ```\n */\nexport function useMutationState<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n options: MutationStateOptions<TResult, TMutation> = {},\n queryClient?: QueryClient,\n): Array<TResult> {\n const mutationCache = useQueryClient(queryClient).getMutationCache()\n const optionsRef = React.useRef(options)\n const resultRef = React.useRef<Array<TResult>>(null)\n if (resultRef.current === null) {\n resultRef.current = getResult(mutationCache, options)\n }\n\n React.useEffect(() => {\n optionsRef.current = options\n })\n\n return React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n mutationCache.subscribe(() => {\n const nextResult = replaceEqualDeep(\n resultRef.current,\n getResult(mutationCache, optionsRef.current),\n )\n if (resultRef.current !== nextResult) {\n resultRef.current = nextResult\n notifyManager.schedule(onStoreChange)\n }\n }),\n [mutationCache],\n ),\n () => resultRef.current,\n () => resultRef.current,\n )!\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,cACd,SACA,aACQ;CACR,MAAM,SAAS,eAAe,WAAW;CACzC,OAAO,iBACL,EAAE,SAAS;EAAE,GAAG;EAAS,QAAQ;CAAU,EAAE,GAC7C,MACF,CAAC,CAAC;AACJ;;;;;;;;AA6BA,SAAS,UAKP,eACA,SACgB;CAChB,OAAO,cACJ,QAAQ,QAAQ,OAAO,CAAC,CACxB,KACE,aACE,QAAQ,SACL,QAAQ,OAAO,QAAqB,IACpC,SAAS,KACjB;AACJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,SAAgB,iBAKd,UAAoD,CAAC,GACrD,aACgB;CAChB,MAAM,gBAAgB,eAAe,WAAW,CAAC,CAAC,iBAAiB;CACnE,MAAM,aAAa,MAAM,OAAO,OAAO;CACvC,MAAM,YAAY,MAAM,OAAuB,IAAI;CACnD,IAAI,UAAU,YAAY,MACxB,UAAU,UAAU,UAAU,eAAe,OAAO;CAGtD,MAAM,gBAAgB;EACpB,WAAW,UAAU;CACvB,CAAC;CAED,OAAO,MAAM,qBACX,MAAM,aACH,kBACC,cAAc,gBAAgB;EAC5B,MAAM,aAAa,iBACjB,UAAU,SACV,UAAU,eAAe,WAAW,OAAO,CAC7C;EACA,IAAI,UAAU,YAAY,YAAY;GACpC,UAAU,UAAU;GACpB,cAAc,SAAS,aAAa;EACtC;CACF,CAAC,GACH,CAAC,aAAa,CAChB,SACM,UAAU,eACV,UAAU,OAClB;AACF"}
|
|
@@ -16,12 +16,9 @@ let _tanstack_query_core = require("@tanstack/query-core");
|
|
|
16
16
|
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
17
17
|
* over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
|
|
18
18
|
* already there or already in flight.
|
|
19
|
-
*
|
|
20
19
|
* @param options - The {@link UsePrefetchInfiniteQueryOptions} to use — everything you can pass to `queryClient.infiniteQuery`.
|
|
21
20
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
22
21
|
* be used.
|
|
23
|
-
* @returns `void` — nothing is returned.
|
|
24
|
-
*
|
|
25
22
|
* @example
|
|
26
23
|
* ```tsx
|
|
27
24
|
* import { Suspense } from 'react'
|