From ca6f52cd57c93eef3e37cd009a90bc0ca845a8ed Mon Sep 17 00:00:00 2001 From: Jonghyeon Ko Date: Wed, 4 Sep 2024 14:14:15 +0900 Subject: [PATCH] feat(react-query): add optional queryClient parameter like other hooks --- packages/react-query/src/prefetch.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/react-query/src/prefetch.ts b/packages/react-query/src/prefetch.ts index 4bc1f9becc..fcda8ecc08 100644 --- a/packages/react-query/src/prefetch.ts +++ b/packages/react-query/src/prefetch.ts @@ -3,6 +3,7 @@ import type { DefaultError, FetchInfiniteQueryOptions, FetchQueryOptions, + QueryClient, QueryKey, } from '@tanstack/query-core' @@ -11,11 +12,14 @@ export function usePrefetchQuery< TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, ->(options: FetchQueryOptions) { - const queryClient = useQueryClient() +>( + options: FetchQueryOptions, + queryClient?: QueryClient, +) { + const client = useQueryClient(queryClient) - if (!queryClient.getQueryState(options.queryKey)) { - queryClient.prefetchQuery(options) + if (!client.getQueryState(options.queryKey)) { + client.prefetchQuery(options) } } @@ -33,10 +37,11 @@ export function usePrefetchInfiniteQuery< TQueryKey, TPageParam >, + queryClient?: QueryClient, ) { - const queryClient = useQueryClient() + const client = useQueryClient(queryClient) - if (!queryClient.getQueryState(options.queryKey)) { - queryClient.prefetchInfiniteQuery(options) + if (!client.getQueryState(options.queryKey)) { + client.prefetchInfiniteQuery(options) } }