SIGN IN SIGN UP
TanStack / query UNCLAIMED

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.

0 0 0 TypeScript
feat(react-query): usePrefetchQuery (#7582) * feat: usePrefetchQuery * refactor: switch to actual prefetching * refactor: remove ensureInfiniteQueryData function will do in a separate PR * chore: add tests for usePrefetchQuery and usePrefetchInfiniteQuery (#7586) * chore: add tests for usePrefetchQuery and usePrefetchInfiniteQuery * chore: update tests to assert the alternative spy is not called * chore: add some new tests * chore: remove it.only whoops * chore: call mockClear after fetching * chore: improve waterfall test by asserting fallback calls instead of loading node query * chore: improve code repetition * chore: add some generics to helper functions * usePrefetchQuery type tests and docs (#7592) * chore: add type tests and docs * chore: update hooks to use FetchQueryOptions and FetchInfiniteQueryOptions * chore: update tests * chore: update docs * chore: remove .md extension from link * chore: add unknown default value to TQueryFnData * Apply suggestions from code review --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc> * Apply suggestions from code review Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com> * chore: fix types in tests * chore: add new tests (#7614) * chore: add new tests * Apply suggestions from code review --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc> --------- Co-authored-by: Bruno Lopes <88719327+brunolopesr@users.noreply.github.com> Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com>
2024-06-25 14:25:28 +02:00
---
id: usePrefetchQuery
title: usePrefetchQuery
---
```tsx
usePrefetchQuery(options)
feat(react-query): usePrefetchQuery (#7582) * feat: usePrefetchQuery * refactor: switch to actual prefetching * refactor: remove ensureInfiniteQueryData function will do in a separate PR * chore: add tests for usePrefetchQuery and usePrefetchInfiniteQuery (#7586) * chore: add tests for usePrefetchQuery and usePrefetchInfiniteQuery * chore: update tests to assert the alternative spy is not called * chore: add some new tests * chore: remove it.only whoops * chore: call mockClear after fetching * chore: improve waterfall test by asserting fallback calls instead of loading node query * chore: improve code repetition * chore: add some generics to helper functions * usePrefetchQuery type tests and docs (#7592) * chore: add type tests and docs * chore: update hooks to use FetchQueryOptions and FetchInfiniteQueryOptions * chore: update tests * chore: update docs * chore: remove .md extension from link * chore: add unknown default value to TQueryFnData * Apply suggestions from code review --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc> * Apply suggestions from code review Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com> * chore: fix types in tests * chore: add new tests (#7614) * chore: add new tests * Apply suggestions from code review --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc> --------- Co-authored-by: Bruno Lopes <88719327+brunolopesr@users.noreply.github.com> Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com>
2024-06-25 14:25:28 +02:00
```
**Options**
You can pass everything to `usePrefetchQuery` that you can pass to [`queryClient.prefetchQuery`](../../../../reference/QueryClient.md#queryclientprefetchquery). Remember that some of them are required as below:
feat(react-query): usePrefetchQuery (#7582) * feat: usePrefetchQuery * refactor: switch to actual prefetching * refactor: remove ensureInfiniteQueryData function will do in a separate PR * chore: add tests for usePrefetchQuery and usePrefetchInfiniteQuery (#7586) * chore: add tests for usePrefetchQuery and usePrefetchInfiniteQuery * chore: update tests to assert the alternative spy is not called * chore: add some new tests * chore: remove it.only whoops * chore: call mockClear after fetching * chore: improve waterfall test by asserting fallback calls instead of loading node query * chore: improve code repetition * chore: add some generics to helper functions * usePrefetchQuery type tests and docs (#7592) * chore: add type tests and docs * chore: update hooks to use FetchQueryOptions and FetchInfiniteQueryOptions * chore: update tests * chore: update docs * chore: remove .md extension from link * chore: add unknown default value to TQueryFnData * Apply suggestions from code review --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc> * Apply suggestions from code review Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com> * chore: fix types in tests * chore: add new tests (#7614) * chore: add new tests * Apply suggestions from code review --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc> --------- Co-authored-by: Bruno Lopes <88719327+brunolopesr@users.noreply.github.com> Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com>
2024-06-25 14:25:28 +02:00
- `queryKey: QueryKey`
- **Required**
- The query key to prefetch during render
- `queryFn: (context: QueryFunctionContext) => Promise<TData>`
- **Required, but only if no default query function has been defined** See [Default Query Function](../../guides/default-query-function.md) for more information.
feat(react-query): usePrefetchQuery (#7582) * feat: usePrefetchQuery * refactor: switch to actual prefetching * refactor: remove ensureInfiniteQueryData function will do in a separate PR * chore: add tests for usePrefetchQuery and usePrefetchInfiniteQuery (#7586) * chore: add tests for usePrefetchQuery and usePrefetchInfiniteQuery * chore: update tests to assert the alternative spy is not called * chore: add some new tests * chore: remove it.only whoops * chore: call mockClear after fetching * chore: improve waterfall test by asserting fallback calls instead of loading node query * chore: improve code repetition * chore: add some generics to helper functions * usePrefetchQuery type tests and docs (#7592) * chore: add type tests and docs * chore: update hooks to use FetchQueryOptions and FetchInfiniteQueryOptions * chore: update tests * chore: update docs * chore: remove .md extension from link * chore: add unknown default value to TQueryFnData * Apply suggestions from code review --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc> * Apply suggestions from code review Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com> * chore: fix types in tests * chore: add new tests (#7614) * chore: add new tests * Apply suggestions from code review --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc> --------- Co-authored-by: Bruno Lopes <88719327+brunolopesr@users.noreply.github.com> Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com>
2024-06-25 14:25:28 +02:00
**Returns**
The `usePrefetchQuery` does not return anything, it should be used just to fire a prefetch during render, before a suspense boundary that wraps a component that uses [`useSuspenseQuery`](../useSuspenseQuery.md).