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

useQueryClient.ts

24 lines677 Bytes

1import { hasInjectionContext, inject } from 'vue-demi'
2​
3import { getClientKey } from './utils'
4import type { QueryClient } from './queryClient'
5​
6export function useQueryClient(id = ''): QueryClient {
7 // ensures that `inject()` can be used
8 if (!hasInjectionContext()) {
9 throw new Error(
10 'vue-query hooks can only be used inside setup() function or functions that support injection context.',
11 )
12 }
13​
14 const key = getClientKey(id)
15 const queryClient = inject<QueryClient>(key)
16​
17 if (!queryClient) {
18 throw new Error(
19 "No 'queryClient' found in Vue context, use 'VueQueryPlugin' to properly initialize the library.",
20 )
21 }
22​
23 return queryClient