diff --git a/packages/react-query/src/__tests__/prefetch.test-d.tsx b/packages/react-query/src/__tests__/prefetch.test-d.tsx index 34ab8bc64b..93196da0c8 100644 --- a/packages/react-query/src/__tests__/prefetch.test-d.tsx +++ b/packages/react-query/src/__tests__/prefetch.test-d.tsx @@ -1,5 +1,5 @@ import { describe, expectTypeOf, it } from 'vitest' -import { usePrefetchInfiniteQuery, usePrefetchQuery } from '../prefetch' +import { usePrefetchInfiniteQuery, usePrefetchQuery } from '..' describe('usePrefetchQuery', () => { it('should return nothing', () => { diff --git a/packages/react-query/src/index.ts b/packages/react-query/src/index.ts index 2450b6a664..7d4724bd97 100644 --- a/packages/react-query/src/index.ts +++ b/packages/react-query/src/index.ts @@ -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, diff --git a/packages/react-query/src/prefetch.ts b/packages/react-query/src/usePrefetchInfiniteQuery.tsx similarity index 61% rename from packages/react-query/src/prefetch.ts rename to packages/react-query/src/usePrefetchInfiniteQuery.tsx index fcda8ecc08..08c2fcdfa3 100644 --- a/packages/react-query/src/prefetch.ts +++ b/packages/react-query/src/usePrefetchInfiniteQuery.tsx @@ -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, - queryClient?: QueryClient, -) { - const client = useQueryClient(queryClient) - - if (!client.getQueryState(options.queryKey)) { - client.prefetchQuery(options) - } -} - export function usePrefetchInfiniteQuery< TQueryFnData = unknown, TError = DefaultError, diff --git a/packages/react-query/src/usePrefetchQuery.tsx b/packages/react-query/src/usePrefetchQuery.tsx new file mode 100644 index 0000000000..54c4cc6499 --- /dev/null +++ b/packages/react-query/src/usePrefetchQuery.tsx @@ -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, + queryClient?: QueryClient, +) { + const client = useQueryClient(queryClient) + + if (!client.getQueryState(options.queryKey)) { + client.prefetchQuery(options) + } +}