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

useQueryClient.ts

27 lines912 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 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
7 * be used.
8 * @returns The current `QueryClient` instance.
9 * @throws {Error} If no `queryClient` argument is passed and no `QueryClientProvider` is found in the
10 * 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()