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

useQueryClient.ts

25 lines639 Bytes

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