Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useQueryClient.ts

29 lines929 Bytes

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