Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react-query): add optional queryClient parameter like other hooks #8014

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading