@tanstack/react-query 5.102.8 → 5.103.1
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
|
@@ -6,6 +6,42 @@ let react = require("react");
|
|
|
6
6
|
react = require_rolldown_runtime.__toESM(react, 1);
|
|
7
7
|
let _tanstack_query_core = require("@tanstack/query-core");
|
|
8
8
|
//#region src/useIsFetching.ts
|
|
9
|
+
/**
|
|
10
|
+
* The `useIsFetching` hook returns the `number` of the queries that your application is loading or fetching in
|
|
11
|
+
* the background (useful for app-wide loading indicators).
|
|
12
|
+
*
|
|
13
|
+
* @param filters - The {@link QueryFilters} to narrow down the matched queries.
|
|
14
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
15
|
+
* be used.
|
|
16
|
+
* @returns Will be the `number` of the queries that your application is currently loading or fetching in the
|
|
17
|
+
* background.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* import { useIsFetching } from '@tanstack/react-query'
|
|
22
|
+
*
|
|
23
|
+
* function PostsFetchingIndicator() {
|
|
24
|
+
* // How many queries matching the posts prefix are fetching?
|
|
25
|
+
* const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
|
|
26
|
+
*
|
|
27
|
+
* return isFetchingPosts ? <span>Refreshing posts...</span> : null
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* A global loading indicator for any query fetching in the background, not just the ones on screen:
|
|
33
|
+
* ```tsx
|
|
34
|
+
* import { useIsFetching } from '@tanstack/react-query'
|
|
35
|
+
*
|
|
36
|
+
* function GlobalLoadingIndicator() {
|
|
37
|
+
* const isFetching = useIsFetching()
|
|
38
|
+
*
|
|
39
|
+
* return isFetching ? (
|
|
40
|
+
* <div>Queries are fetching in the background...</div>
|
|
41
|
+
* ) : null
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
9
45
|
function useIsFetching(filters, queryClient) {
|
|
10
46
|
const client = require_QueryClientProvider.useQueryClient(queryClient);
|
|
11
47
|
const queryCache = client.getQueryCache();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useIsFetching.cjs","names":["useQueryClient","React","notifyManager"],"sources":["../../src/useIsFetching.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\nimport { notifyManager } from '@tanstack/query-core'\n\nimport { useQueryClient } from './QueryClientProvider'\nimport type { QueryClient, QueryFilters } from '@tanstack/query-core'\n\nexport function useIsFetching(\n filters?: QueryFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n const queryCache = client.getQueryCache()\n\n return React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n queryCache.subscribe(notifyManager.batchCalls(onStoreChange)),\n [queryCache],\n ),\n () => client.isFetching(filters),\n () => client.isFetching(filters),\n )\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useIsFetching.cjs","names":["useQueryClient","React","notifyManager"],"sources":["../../src/useIsFetching.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\nimport { notifyManager } from '@tanstack/query-core'\n\nimport { useQueryClient } from './QueryClientProvider'\nimport type { QueryClient, QueryFilters } from '@tanstack/query-core'\n\n/**\n * The `useIsFetching` hook returns the `number` of the queries that your application is loading or fetching in\n * the background (useful for app-wide loading indicators).\n *\n * @param filters - The {@link QueryFilters} to narrow down the matched queries.\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 queries that your application is currently loading or fetching in the\n * background.\n *\n * @example\n * ```tsx\n * import { useIsFetching } from '@tanstack/react-query'\n *\n * function PostsFetchingIndicator() {\n * // How many queries matching the posts prefix are fetching?\n * const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })\n *\n * return isFetchingPosts ? <span>Refreshing posts...</span> : null\n * }\n * ```\n *\n * @example\n * A global loading indicator for any query fetching in the background, not just the ones on screen:\n * ```tsx\n * import { useIsFetching } from '@tanstack/react-query'\n *\n * function GlobalLoadingIndicator() {\n * const isFetching = useIsFetching()\n *\n * return isFetching ? (\n * <div>Queries are fetching in the background...</div>\n * ) : null\n * }\n * ```\n */\nexport function useIsFetching(\n filters?: QueryFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n const queryCache = client.getQueryCache()\n\n return React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n queryCache.subscribe(notifyManager.batchCalls(onStoreChange)),\n [queryCache],\n ),\n () => client.isFetching(filters),\n () => client.isFetching(filters),\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,SAAgB,cACd,SACA,aACQ;CACR,MAAM,SAASA,4BAAAA,eAAe,WAAW;CACzC,MAAM,aAAa,OAAO,cAAc;CAExC,OAAOC,MAAM,qBACXA,MAAM,aACH,kBACC,WAAW,UAAUC,qBAAAA,cAAc,WAAW,aAAa,CAAC,GAC9D,CAAC,UAAU,CACb,SACM,OAAO,WAAW,OAAO,SACzB,OAAO,WAAW,OAAO,CACjC;AACF"}
|
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
import { QueryClient, QueryFilters } from "@tanstack/query-core";
|
|
2
2
|
//#region src/useIsFetching.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* The `useIsFetching` hook returns the `number` of the queries that your application is loading or fetching in
|
|
5
|
+
* the background (useful for app-wide loading indicators).
|
|
6
|
+
*
|
|
7
|
+
* @param filters - The {@link QueryFilters} to narrow down the matched queries.
|
|
8
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
9
|
+
* be used.
|
|
10
|
+
* @returns Will be the `number` of the queries that your application is currently loading or fetching in the
|
|
11
|
+
* background.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* import { useIsFetching } from '@tanstack/react-query'
|
|
16
|
+
*
|
|
17
|
+
* function PostsFetchingIndicator() {
|
|
18
|
+
* // How many queries matching the posts prefix are fetching?
|
|
19
|
+
* const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
|
|
20
|
+
*
|
|
21
|
+
* return isFetchingPosts ? <span>Refreshing posts...</span> : null
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* A global loading indicator for any query fetching in the background, not just the ones on screen:
|
|
27
|
+
* ```tsx
|
|
28
|
+
* import { useIsFetching } from '@tanstack/react-query'
|
|
29
|
+
*
|
|
30
|
+
* function GlobalLoadingIndicator() {
|
|
31
|
+
* const isFetching = useIsFetching()
|
|
32
|
+
*
|
|
33
|
+
* return isFetching ? (
|
|
34
|
+
* <div>Queries are fetching in the background...</div>
|
|
35
|
+
* ) : null
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
3
39
|
declare function useIsFetching(filters?: QueryFilters, queryClient?: QueryClient): number;
|
|
4
40
|
//#endregion
|
|
5
41
|
export { useIsFetching };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useIsFetching.d.cts","names":[],"sources":["../../src/useIsFetching.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useIsFetching.d.cts","names":[],"sources":["../../src/useIsFetching.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2CgB,cACd,UAAU,cACV,cAAc"}
|
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
import { QueryClient, QueryFilters } from "@tanstack/query-core";
|
|
2
2
|
//#region src/useIsFetching.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* The `useIsFetching` hook returns the `number` of the queries that your application is loading or fetching in
|
|
5
|
+
* the background (useful for app-wide loading indicators).
|
|
6
|
+
*
|
|
7
|
+
* @param filters - The {@link QueryFilters} to narrow down the matched queries.
|
|
8
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
9
|
+
* be used.
|
|
10
|
+
* @returns Will be the `number` of the queries that your application is currently loading or fetching in the
|
|
11
|
+
* background.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* import { useIsFetching } from '@tanstack/react-query'
|
|
16
|
+
*
|
|
17
|
+
* function PostsFetchingIndicator() {
|
|
18
|
+
* // How many queries matching the posts prefix are fetching?
|
|
19
|
+
* const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
|
|
20
|
+
*
|
|
21
|
+
* return isFetchingPosts ? <span>Refreshing posts...</span> : null
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* A global loading indicator for any query fetching in the background, not just the ones on screen:
|
|
27
|
+
* ```tsx
|
|
28
|
+
* import { useIsFetching } from '@tanstack/react-query'
|
|
29
|
+
*
|
|
30
|
+
* function GlobalLoadingIndicator() {
|
|
31
|
+
* const isFetching = useIsFetching()
|
|
32
|
+
*
|
|
33
|
+
* return isFetching ? (
|
|
34
|
+
* <div>Queries are fetching in the background...</div>
|
|
35
|
+
* ) : null
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
3
39
|
declare function useIsFetching(filters?: QueryFilters, queryClient?: QueryClient): number;
|
|
4
40
|
//#endregion
|
|
5
41
|
export { useIsFetching };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useIsFetching.d.ts","names":[],"sources":["../../src/useIsFetching.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useIsFetching.d.ts","names":[],"sources":["../../src/useIsFetching.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2CgB,cACd,UAAU,cACV,cAAc"}
|
|
@@ -3,6 +3,42 @@ import { useQueryClient } from "./QueryClientProvider.js";
|
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
import { notifyManager } from "@tanstack/query-core";
|
|
5
5
|
//#region src/useIsFetching.ts
|
|
6
|
+
/**
|
|
7
|
+
* The `useIsFetching` hook returns the `number` of the queries that your application is loading or fetching in
|
|
8
|
+
* the background (useful for app-wide loading indicators).
|
|
9
|
+
*
|
|
10
|
+
* @param filters - The {@link QueryFilters} to narrow down the matched queries.
|
|
11
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
12
|
+
* be used.
|
|
13
|
+
* @returns Will be the `number` of the queries that your application is currently loading or fetching in the
|
|
14
|
+
* background.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* import { useIsFetching } from '@tanstack/react-query'
|
|
19
|
+
*
|
|
20
|
+
* function PostsFetchingIndicator() {
|
|
21
|
+
* // How many queries matching the posts prefix are fetching?
|
|
22
|
+
* const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
|
|
23
|
+
*
|
|
24
|
+
* return isFetchingPosts ? <span>Refreshing posts...</span> : null
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* A global loading indicator for any query fetching in the background, not just the ones on screen:
|
|
30
|
+
* ```tsx
|
|
31
|
+
* import { useIsFetching } from '@tanstack/react-query'
|
|
32
|
+
*
|
|
33
|
+
* function GlobalLoadingIndicator() {
|
|
34
|
+
* const isFetching = useIsFetching()
|
|
35
|
+
*
|
|
36
|
+
* return isFetching ? (
|
|
37
|
+
* <div>Queries are fetching in the background...</div>
|
|
38
|
+
* ) : null
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
6
42
|
function useIsFetching(filters, queryClient) {
|
|
7
43
|
const client = useQueryClient(queryClient);
|
|
8
44
|
const queryCache = client.getQueryCache();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useIsFetching.js","names":[],"sources":["../../src/useIsFetching.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\nimport { notifyManager } from '@tanstack/query-core'\n\nimport { useQueryClient } from './QueryClientProvider'\nimport type { QueryClient, QueryFilters } from '@tanstack/query-core'\n\nexport function useIsFetching(\n filters?: QueryFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n const queryCache = client.getQueryCache()\n\n return React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n queryCache.subscribe(notifyManager.batchCalls(onStoreChange)),\n [queryCache],\n ),\n () => client.isFetching(filters),\n () => client.isFetching(filters),\n )\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useIsFetching.js","names":[],"sources":["../../src/useIsFetching.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\nimport { notifyManager } from '@tanstack/query-core'\n\nimport { useQueryClient } from './QueryClientProvider'\nimport type { QueryClient, QueryFilters } from '@tanstack/query-core'\n\n/**\n * The `useIsFetching` hook returns the `number` of the queries that your application is loading or fetching in\n * the background (useful for app-wide loading indicators).\n *\n * @param filters - The {@link QueryFilters} to narrow down the matched queries.\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 queries that your application is currently loading or fetching in the\n * background.\n *\n * @example\n * ```tsx\n * import { useIsFetching } from '@tanstack/react-query'\n *\n * function PostsFetchingIndicator() {\n * // How many queries matching the posts prefix are fetching?\n * const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })\n *\n * return isFetchingPosts ? <span>Refreshing posts...</span> : null\n * }\n * ```\n *\n * @example\n * A global loading indicator for any query fetching in the background, not just the ones on screen:\n * ```tsx\n * import { useIsFetching } from '@tanstack/react-query'\n *\n * function GlobalLoadingIndicator() {\n * const isFetching = useIsFetching()\n *\n * return isFetching ? (\n * <div>Queries are fetching in the background...</div>\n * ) : null\n * }\n * ```\n */\nexport function useIsFetching(\n filters?: QueryFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n const queryCache = client.getQueryCache()\n\n return React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n queryCache.subscribe(notifyManager.batchCalls(onStoreChange)),\n [queryCache],\n ),\n () => client.isFetching(filters),\n () => client.isFetching(filters),\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,SAAgB,cACd,SACA,aACQ;CACR,MAAM,SAAS,eAAe,WAAW;CACzC,MAAM,aAAa,OAAO,cAAc;CAExC,OAAO,MAAM,qBACX,MAAM,aACH,kBACC,WAAW,UAAU,cAAc,WAAW,aAAa,CAAC,GAC9D,CAAC,UAAU,CACb,SACM,OAAO,WAAW,OAAO,SACzB,OAAO,WAAW,OAAO,CACjC;AACF"}
|
|
@@ -6,6 +6,178 @@ let react = require("react");
|
|
|
6
6
|
react = require_rolldown_runtime.__toESM(react, 1);
|
|
7
7
|
let _tanstack_query_core = require("@tanstack/query-core");
|
|
8
8
|
//#region src/useMutation.ts
|
|
9
|
+
/**
|
|
10
|
+
* Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
|
|
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
|
+
* @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.
|
|
16
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
17
|
+
* be used.
|
|
18
|
+
* @returns `mutate`/`mutateAsync` also accept per-call `onSuccess`/`onError`/`onSettled` callbacks as a second
|
|
19
|
+
* argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared
|
|
20
|
+
* mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call callbacks
|
|
21
|
+
* fire only for the latest call you've made, and only while the component is still mounted — unmounting before
|
|
22
|
+
* the mutation settles removes the subscription and prevents them from firing.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
27
|
+
*
|
|
28
|
+
* function AddTodo() {
|
|
29
|
+
* const queryClient = useQueryClient()
|
|
30
|
+
*
|
|
31
|
+
* const addMutation = useMutation({
|
|
32
|
+
* mutationFn: addTodo,
|
|
33
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
34
|
+
* })
|
|
35
|
+
*
|
|
36
|
+
* return (
|
|
37
|
+
* <button
|
|
38
|
+
* onClick={() =>
|
|
39
|
+
* addMutation.mutate('Item', {
|
|
40
|
+
* onError: (error) => console.error('Failed to add item:', error),
|
|
41
|
+
* })
|
|
42
|
+
* }
|
|
43
|
+
* >
|
|
44
|
+
* Add
|
|
45
|
+
* </button>
|
|
46
|
+
* )
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* Rendering the mutation's own state, rather than just firing it off:
|
|
52
|
+
* ```tsx
|
|
53
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
54
|
+
*
|
|
55
|
+
* function AddTodo() {
|
|
56
|
+
* const queryClient = useQueryClient()
|
|
57
|
+
*
|
|
58
|
+
* const addMutation = useMutation({
|
|
59
|
+
* mutationFn: addTodo,
|
|
60
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
61
|
+
* })
|
|
62
|
+
*
|
|
63
|
+
* return (
|
|
64
|
+
* <div>
|
|
65
|
+
* {addMutation.isPending ? (
|
|
66
|
+
* 'Adding todo...'
|
|
67
|
+
* ) : (
|
|
68
|
+
* <>
|
|
69
|
+
* {addMutation.isError ? (
|
|
70
|
+
* <div>An error occurred: {addMutation.error.message}</div>
|
|
71
|
+
* ) : null}
|
|
72
|
+
* <button onClick={() => addMutation.mutate('Item')}>Add</button>
|
|
73
|
+
* </>
|
|
74
|
+
* )}
|
|
75
|
+
* </div>
|
|
76
|
+
* )
|
|
77
|
+
* }
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* Optimistic update via `onMutate`, rolling back on `onError`:
|
|
82
|
+
* ```tsx
|
|
83
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
84
|
+
*
|
|
85
|
+
* function AddTodo() {
|
|
86
|
+
* const queryClient = useQueryClient()
|
|
87
|
+
*
|
|
88
|
+
* const addMutation = useMutation({
|
|
89
|
+
* mutationFn: addTodo,
|
|
90
|
+
* onMutate: async (newTodo) => {
|
|
91
|
+
* await queryClient.cancelQueries({ queryKey: ['todos'] })
|
|
92
|
+
* const previousTodos = queryClient.getQueryData<Array<string>>(['todos'])
|
|
93
|
+
*
|
|
94
|
+
* queryClient.setQueryData<Array<string>>(['todos'], (old) => [
|
|
95
|
+
* ...(old ?? []),
|
|
96
|
+
* newTodo,
|
|
97
|
+
* ])
|
|
98
|
+
*
|
|
99
|
+
* // Passed to `onError` as `onMutateResult` if the mutation fails.
|
|
100
|
+
* return { previousTodos }
|
|
101
|
+
* },
|
|
102
|
+
* onError: (_err, _newTodo, onMutateResult) => {
|
|
103
|
+
* queryClient.setQueryData(['todos'], onMutateResult?.previousTodos)
|
|
104
|
+
* },
|
|
105
|
+
* onSettled: () => {
|
|
106
|
+
* queryClient.invalidateQueries({ queryKey: ['todos'] })
|
|
107
|
+
* },
|
|
108
|
+
* })
|
|
109
|
+
*
|
|
110
|
+
* return (
|
|
111
|
+
* <button onClick={() => addMutation.mutate('Item')}>Add</button>
|
|
112
|
+
* )
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
|
|
118
|
+
* promise per call instead, so you can wait for all of them when they succeed:
|
|
119
|
+
* ```tsx
|
|
120
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
121
|
+
*
|
|
122
|
+
* function AddTodos() {
|
|
123
|
+
* const queryClient = useQueryClient()
|
|
124
|
+
*
|
|
125
|
+
* const addMutation = useMutation({
|
|
126
|
+
* mutationFn: addTodo,
|
|
127
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
128
|
+
* })
|
|
129
|
+
*
|
|
130
|
+
* async function handleAddAll(todos: Array<string>) {
|
|
131
|
+
* try {
|
|
132
|
+
* await Promise.all(todos.map((todo) => addMutation.mutateAsync(todo)))
|
|
133
|
+
* } catch (error) {
|
|
134
|
+
* console.error('Failed to add todos:', error)
|
|
135
|
+
* }
|
|
136
|
+
* }
|
|
137
|
+
*
|
|
138
|
+
* return (
|
|
139
|
+
* <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>
|
|
140
|
+
* Add all
|
|
141
|
+
* </button>
|
|
142
|
+
* )
|
|
143
|
+
* }
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* If some of the mutations above can fail independently of the others, and you want to know which ones
|
|
148
|
+
* did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
|
|
149
|
+
* `Promise.allSettled`:
|
|
150
|
+
* ```tsx
|
|
151
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
152
|
+
*
|
|
153
|
+
* function AddTodos() {
|
|
154
|
+
* const queryClient = useQueryClient()
|
|
155
|
+
*
|
|
156
|
+
* const addMutation = useMutation({
|
|
157
|
+
* mutationFn: addTodo,
|
|
158
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
159
|
+
* })
|
|
160
|
+
*
|
|
161
|
+
* async function handleAddAll(todos: Array<string>) {
|
|
162
|
+
* const addResults = await Promise.allSettled(
|
|
163
|
+
* todos.map((todo) => addMutation.mutateAsync(todo)),
|
|
164
|
+
* )
|
|
165
|
+
*
|
|
166
|
+
* addResults.forEach((addResult, index) => {
|
|
167
|
+
* if (addResult.status === 'rejected') {
|
|
168
|
+
* console.error(`Failed to add "${todos[index]}":`, addResult.reason)
|
|
169
|
+
* }
|
|
170
|
+
* })
|
|
171
|
+
* }
|
|
172
|
+
*
|
|
173
|
+
* return (
|
|
174
|
+
* <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>
|
|
175
|
+
* Add all
|
|
176
|
+
* </button>
|
|
177
|
+
* )
|
|
178
|
+
* }
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
9
181
|
function useMutation(options, queryClient) {
|
|
10
182
|
const client = require_QueryClientProvider.useQueryClient(queryClient);
|
|
11
183
|
const [observer] = react.useState(() => new _tanstack_query_core.MutationObserver(client, options));
|
|
@@ -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\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":"
|
|
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 *\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 * @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 *\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 *\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 *\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 *\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 *\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8LA,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"}
|
|
@@ -1,6 +1,178 @@
|
|
|
1
1
|
import { UseMutationOptions, UseMutationResult } from "./types.cjs";
|
|
2
2
|
import { DefaultError, QueryClient } from "@tanstack/query-core";
|
|
3
3
|
//#region src/useMutation.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
|
|
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
|
+
* @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.
|
|
11
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
12
|
+
* be used.
|
|
13
|
+
* @returns `mutate`/`mutateAsync` also accept per-call `onSuccess`/`onError`/`onSettled` callbacks as a second
|
|
14
|
+
* argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared
|
|
15
|
+
* mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call callbacks
|
|
16
|
+
* fire only for the latest call you've made, and only while the component is still mounted — unmounting before
|
|
17
|
+
* the mutation settles removes the subscription and prevents them from firing.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
22
|
+
*
|
|
23
|
+
* function AddTodo() {
|
|
24
|
+
* const queryClient = useQueryClient()
|
|
25
|
+
*
|
|
26
|
+
* const addMutation = useMutation({
|
|
27
|
+
* mutationFn: addTodo,
|
|
28
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
* return (
|
|
32
|
+
* <button
|
|
33
|
+
* onClick={() =>
|
|
34
|
+
* addMutation.mutate('Item', {
|
|
35
|
+
* onError: (error) => console.error('Failed to add item:', error),
|
|
36
|
+
* })
|
|
37
|
+
* }
|
|
38
|
+
* >
|
|
39
|
+
* Add
|
|
40
|
+
* </button>
|
|
41
|
+
* )
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* Rendering the mutation's own state, rather than just firing it off:
|
|
47
|
+
* ```tsx
|
|
48
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
49
|
+
*
|
|
50
|
+
* function AddTodo() {
|
|
51
|
+
* const queryClient = useQueryClient()
|
|
52
|
+
*
|
|
53
|
+
* const addMutation = useMutation({
|
|
54
|
+
* mutationFn: addTodo,
|
|
55
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
56
|
+
* })
|
|
57
|
+
*
|
|
58
|
+
* return (
|
|
59
|
+
* <div>
|
|
60
|
+
* {addMutation.isPending ? (
|
|
61
|
+
* 'Adding todo...'
|
|
62
|
+
* ) : (
|
|
63
|
+
* <>
|
|
64
|
+
* {addMutation.isError ? (
|
|
65
|
+
* <div>An error occurred: {addMutation.error.message}</div>
|
|
66
|
+
* ) : null}
|
|
67
|
+
* <button onClick={() => addMutation.mutate('Item')}>Add</button>
|
|
68
|
+
* </>
|
|
69
|
+
* )}
|
|
70
|
+
* </div>
|
|
71
|
+
* )
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* Optimistic update via `onMutate`, rolling back on `onError`:
|
|
77
|
+
* ```tsx
|
|
78
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
79
|
+
*
|
|
80
|
+
* function AddTodo() {
|
|
81
|
+
* const queryClient = useQueryClient()
|
|
82
|
+
*
|
|
83
|
+
* const addMutation = useMutation({
|
|
84
|
+
* mutationFn: addTodo,
|
|
85
|
+
* onMutate: async (newTodo) => {
|
|
86
|
+
* await queryClient.cancelQueries({ queryKey: ['todos'] })
|
|
87
|
+
* const previousTodos = queryClient.getQueryData<Array<string>>(['todos'])
|
|
88
|
+
*
|
|
89
|
+
* queryClient.setQueryData<Array<string>>(['todos'], (old) => [
|
|
90
|
+
* ...(old ?? []),
|
|
91
|
+
* newTodo,
|
|
92
|
+
* ])
|
|
93
|
+
*
|
|
94
|
+
* // Passed to `onError` as `onMutateResult` if the mutation fails.
|
|
95
|
+
* return { previousTodos }
|
|
96
|
+
* },
|
|
97
|
+
* onError: (_err, _newTodo, onMutateResult) => {
|
|
98
|
+
* queryClient.setQueryData(['todos'], onMutateResult?.previousTodos)
|
|
99
|
+
* },
|
|
100
|
+
* onSettled: () => {
|
|
101
|
+
* queryClient.invalidateQueries({ queryKey: ['todos'] })
|
|
102
|
+
* },
|
|
103
|
+
* })
|
|
104
|
+
*
|
|
105
|
+
* return (
|
|
106
|
+
* <button onClick={() => addMutation.mutate('Item')}>Add</button>
|
|
107
|
+
* )
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
|
|
113
|
+
* promise per call instead, so you can wait for all of them when they succeed:
|
|
114
|
+
* ```tsx
|
|
115
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
116
|
+
*
|
|
117
|
+
* function AddTodos() {
|
|
118
|
+
* const queryClient = useQueryClient()
|
|
119
|
+
*
|
|
120
|
+
* const addMutation = useMutation({
|
|
121
|
+
* mutationFn: addTodo,
|
|
122
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
123
|
+
* })
|
|
124
|
+
*
|
|
125
|
+
* async function handleAddAll(todos: Array<string>) {
|
|
126
|
+
* try {
|
|
127
|
+
* await Promise.all(todos.map((todo) => addMutation.mutateAsync(todo)))
|
|
128
|
+
* } catch (error) {
|
|
129
|
+
* console.error('Failed to add todos:', error)
|
|
130
|
+
* }
|
|
131
|
+
* }
|
|
132
|
+
*
|
|
133
|
+
* return (
|
|
134
|
+
* <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>
|
|
135
|
+
* Add all
|
|
136
|
+
* </button>
|
|
137
|
+
* )
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* If some of the mutations above can fail independently of the others, and you want to know which ones
|
|
143
|
+
* did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
|
|
144
|
+
* `Promise.allSettled`:
|
|
145
|
+
* ```tsx
|
|
146
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
147
|
+
*
|
|
148
|
+
* function AddTodos() {
|
|
149
|
+
* const queryClient = useQueryClient()
|
|
150
|
+
*
|
|
151
|
+
* const addMutation = useMutation({
|
|
152
|
+
* mutationFn: addTodo,
|
|
153
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
154
|
+
* })
|
|
155
|
+
*
|
|
156
|
+
* async function handleAddAll(todos: Array<string>) {
|
|
157
|
+
* const addResults = await Promise.allSettled(
|
|
158
|
+
* todos.map((todo) => addMutation.mutateAsync(todo)),
|
|
159
|
+
* )
|
|
160
|
+
*
|
|
161
|
+
* addResults.forEach((addResult, index) => {
|
|
162
|
+
* if (addResult.status === 'rejected') {
|
|
163
|
+
* console.error(`Failed to add "${todos[index]}":`, addResult.reason)
|
|
164
|
+
* }
|
|
165
|
+
* })
|
|
166
|
+
* }
|
|
167
|
+
*
|
|
168
|
+
* return (
|
|
169
|
+
* <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>
|
|
170
|
+
* Add all
|
|
171
|
+
* </button>
|
|
172
|
+
* )
|
|
173
|
+
* }
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
4
176
|
declare function useMutation<TData = unknown, TError = DefaultError, TVariables = void, TOnMutateResult = unknown>(options: UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, queryClient?: QueryClient): UseMutationResult<TData, TError, TVariables, TOnMutateResult>;
|
|
5
177
|
//#endregion
|
|
6
178
|
export { useMutation };
|
|
@@ -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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA8LgB,YACd,iBACA,SAAS,cACT,mBACA,2BAEA,SAAS,mBAAmB,OAAO,QAAQ,YAAY,kBACvD,cAAc,cACb,kBAAkB,OAAO,QAAQ,YAAY"}
|