Skip to content

Commit

Permalink
feat(react-query): add optional queryClient parameter to usePrefetchQ…
Browse files Browse the repository at this point in the history
…uery (#8014)

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
  • Loading branch information
manudeli and TkDodo committed Sep 6, 2024
1 parent 6799b82 commit 48702e4
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/react-query/src/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
DefaultError,
FetchInfiniteQueryOptions,
FetchQueryOptions,
QueryClient,
QueryKey,
} from '@tanstack/query-core'

Expand All @@ -11,11 +12,14 @@ export function usePrefetchQuery<
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(options: FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>) {
const queryClient = useQueryClient()
>(
options: FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
queryClient?: QueryClient,
) {
const client = useQueryClient(queryClient)

if (!queryClient.getQueryState(options.queryKey)) {
queryClient.prefetchQuery(options)
if (!client.getQueryState(options.queryKey)) {
client.prefetchQuery(options)
}
}

Expand All @@ -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)
}
}

0 comments on commit 48702e4

Please sign in to comment.