Open History to see the last commit
1import { getQueryClientContext } from './context.js'
2import type { QueryClient } from '@tanstack/query-core'
3β
4/**
5 * The `useQueryClient` function returns the current `QueryClient` instance.
6 *
7 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
8 * be used.
9 * @returns The current `QueryClient` instance.
10 * @throws If no `queryClient` argument is passed and no `QueryClientProvider` is found in the component tree.
11 * @example
12 * ```svelte
13 * <script lang="ts">
14 * import { useQueryClient } from '@tanstack/svelte-query'
15 *
16 * const queryClient = useQueryClient()
17 *
18 * function refreshPosts() {
19 * queryClient.invalidateQueries({ queryKey: ['posts'] })
20 * }
21 * </script>
22 * ```
23 */
24export function useQueryClient(queryClient?: QueryClient): QueryClient {
25 if (queryClient) return queryClient
26 return getQueryClientContext()
27}