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

useQueryClient.ts

23 lines601 Bytes

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