Skip to content

Commit 26b076c

Browse files
committed
refactor: add dev warning for effectScope
1 parent 964fc87 commit 26b076c

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

packages/vue-query/src/useBaseQuery.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
computed,
3+
getCurrentScope,
34
onScopeDispose,
45
reactive,
56
readonly,
@@ -58,6 +59,14 @@ export function useBaseQuery<
5859
| UseQueryOptionsGeneric<TQueryFnData, TError, TData, TQueryKey> = {},
5960
arg3: UseQueryOptionsGeneric<TQueryFnData, TError, TData, TQueryKey> = {},
6061
): UseQueryReturnType<TData, TError> {
62+
if (process.env.NODE_ENV === 'development') {
63+
if (!getCurrentScope()) {
64+
console.warn(
65+
'vue-query composables like "uesQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
66+
)
67+
}
68+
}
69+
6170
const options = computed(() => parseQueryArgs(arg1, arg2, arg3))
6271

6372
const queryClient =

packages/vue-query/src/useQueryClient.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import { getCurrentScope, hasInjectionContext, inject } from 'vue-demi'
1+
import { hasInjectionContext, inject } from 'vue-demi'
22

33
import { getClientKey } from './utils'
44
import type { QueryClient } from './queryClient'
55

66
export function useQueryClient(id = ''): QueryClient {
7-
if (
8-
// ensures that `inject()` can be used
9-
!hasInjectionContext() ||
10-
// ensures `ref()`, `onScopeDispose()` and other APIs can be used
11-
!getCurrentScope()
12-
) {
13-
throw new Error('vue-query hooks can only be used inside setup() function.')
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+
)
1412
}
1513

1614
const key = getClientKey(id)

0 commit comments

Comments
 (0)