Skip to content

Commit 48702e4

Browse files
manudeliTkDodo
andauthored
feat(react-query): add optional queryClient parameter to usePrefetchQuery (#8014)
Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent 6799b82 commit 48702e4

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

packages/react-query/src/prefetch.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
DefaultError,
44
FetchInfiniteQueryOptions,
55
FetchQueryOptions,
6+
QueryClient,
67
QueryKey,
78
} from '@tanstack/query-core'
89

@@ -11,11 +12,14 @@ export function usePrefetchQuery<
1112
TError = DefaultError,
1213
TData = TQueryFnData,
1314
TQueryKey extends QueryKey = QueryKey,
14-
>(options: FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>) {
15-
const queryClient = useQueryClient()
15+
>(
16+
options: FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
17+
queryClient?: QueryClient,
18+
) {
19+
const client = useQueryClient(queryClient)
1620

17-
if (!queryClient.getQueryState(options.queryKey)) {
18-
queryClient.prefetchQuery(options)
21+
if (!client.getQueryState(options.queryKey)) {
22+
client.prefetchQuery(options)
1923
}
2024
}
2125

@@ -33,10 +37,11 @@ export function usePrefetchInfiniteQuery<
3337
TQueryKey,
3438
TPageParam
3539
>,
40+
queryClient?: QueryClient,
3641
) {
37-
const queryClient = useQueryClient()
42+
const client = useQueryClient(queryClient)
3843

39-
if (!queryClient.getQueryState(options.queryKey)) {
40-
queryClient.prefetchInfiniteQuery(options)
44+
if (!client.getQueryState(options.queryKey)) {
45+
client.prefetchInfiniteQuery(options)
4146
}
4247
}

0 commit comments

Comments
 (0)