Skip to content

Commit

Permalink
fix(react-query): split prefetch.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
manudeli committed Sep 6, 2024
1 parent 353e4ad commit 4329b2d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
3 changes: 2 additions & 1 deletion packages/react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export type {
SuspenseQueriesResults,
SuspenseQueriesOptions,
} from './useSuspenseQueries'
export { usePrefetchQuery, usePrefetchInfiniteQuery } from './prefetch'
export { usePrefetchQuery } from './usePrefetchQuery'
export { usePrefetchInfiniteQuery } from './usePrefetchInfiniteQuery'
export { queryOptions } from './queryOptions'
export type {
DefinedInitialDataOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,10 @@ import { useQueryClient } from './QueryClientProvider'
import type {
DefaultError,
FetchInfiniteQueryOptions,
FetchQueryOptions,
QueryClient,
QueryKey,
} from '@tanstack/query-core'

export function usePrefetchQuery<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
queryClient?: QueryClient,
) {
const client = useQueryClient(queryClient)

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

export function usePrefetchInfiniteQuery<
TQueryFnData = unknown,
TError = DefaultError,
Expand Down
23 changes: 23 additions & 0 deletions packages/react-query/src/usePrefetchQuery.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useQueryClient } from './QueryClientProvider'
import type {
DefaultError,
FetchQueryOptions,
QueryClient,
QueryKey,
} from '@tanstack/query-core'

export function usePrefetchQuery<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
queryClient?: QueryClient,
) {
const client = useQueryClient(queryClient)

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

0 comments on commit 4329b2d

Please sign in to comment.