Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useQueries.md

145 lines6.1 KB

1---
2id: useQueries
3title: useQueries
4---
5​
6```ts
7function useQueries<T, TCombinedResult>(options: object, queryClient?: QueryClient): TCombinedResult;
8```
9​
10Defined in: [packages/preact-query/src/useQueries.ts:304](https://github.com/TanStack/query/blob/main/packages/preact-query/src/useQueries.ts#L304)
11​
12The `useQueries` hook can be used to fetch a variable number of queries.
13​
14The `queries` key accepts an array with query option objects mostly identical to `useQuery` β€” see the
15`queries` parameter below for the differences. A custom `QueryClient` is supplied once, as `useQueries`' own
16top-level second argument, rather than per query.
17​
18Having the same query key more than once in the array of query objects may cause some data to be shared
19between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
20structure.
21​
22The `combine` option can be used to combine the results of the queries into a single value. The result will
23be structurally shared to be as referentially stable as possible.
24​
25## Type Parameters
26​
27### T
28​
29`T` *extends* `any`[]
30​
31### TCombinedResult
32​
33`TCombinedResult` = `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetUseQueryResult`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseQueryResult`\<`Head`\>, `GetUseQueryResult`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseQueryResult`\<`Head`\>, `GetUseQueryResult`\<`Head`\>, `GetUseQueryResult`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...(...)[]`\] *extends* \[\] ? \[\] : ... *extends* ... ? ... : ... : \[`...{ [K in (...)]: (...) }[]`\] : \[...\{ \[K in string \| number \| symbol\]: GetUseQueryResult\<Tails\[K\]\> \}\[\]\] : \{ \[K in string \| number \| symbol\]: GetUseQueryResult\<T\[K\]\> \}
34​
35## Parameters
36​
37### options
38​
39The `queries` array to run, and the optional `combine` and `subscribed` options.
40​
41#### combine?
42​
43(`result`: `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetUseQueryResult`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseQueryResult`\<`Head`\>, `GetUseQueryResult`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...(...)[]`\] *extends* \[\] ? \[\] : ... *extends* ... ? ... : ... : \[`...{ [K in (...)]: (...) }[]`\] : \{ \[K in string \| number \| symbol\]: GetUseQueryResult\<T\[K\]\> \}) => `TCombinedResult`
44​
45Use this to combine the results of the queries into a single value. The result will be structurally
46shared to be as referentially stable as possible.
47​
48#### queries
49​
50 \| readonly \[`T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetUseQueryOptionsForUseQueries`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseQueryOptionsForUseQueries`\<`Head`\>, `GetUseQueryOptionsForUseQueries`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...(...)[]`\] *extends* \[\] ? \[\] : ... *extends* ... ? ... : ... : readonly ...[] *extends* \[`...(...)[]`\] ? \[`...(...)[]`\] : ... *extends* ... ? ... : ... : readonly `unknown`[] *extends* `T` ? `T` : `T` *extends* `UseQueryOptionsForUseQueries`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>[] ? `UseQueryOptionsForUseQueries`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>[] : `UseQueryOptionsForUseQueries`\<`unknown`, `Error`, `unknown`, readonly ...[]\>[]\]
51 \| readonly \[\{ \[K in string \| number \| symbol\]: GetUseQueryOptionsForUseQueries\<T\[K\]\> \}\]
52​
53An array with query option objects, mostly identical to `useQuery` β€” except that `queryClient` and
54`subscribed` aren't accepted per-query (`subscribed` is a top-level option here instead), and
55`placeholderData` accepts a [QueriesPlaceholderDataFunction](../type-aliases/QueriesPlaceholderDataFunction.md), which is called with `previousData`
56and `previousQuery` always `undefined`, rather than `useQuery`'s placeholder function.
57​
58#### subscribed?
59​
60`boolean`
61​
62Set this to `false` to unsubscribe this observer from updates to the query cache.
63​
64**Default Value**
65​
66```ts
67true
68```
69​
70### queryClient?
71​
72[`QueryClient`](../classes/QueryClient.md)
73​
74Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
75will be used.
76​
77## Returns
78​
79`TCombinedResult`
80​
81The combined result. Without `combine`, this is an array with all the query results, in the same
82order as the input. When `combine` is provided, this is the value returned by `combine` instead.
83​
84## Throws
85​
86The error of the first query that should be thrown to the nearest error boundary (see
87`throwOnError`). While suspending, it throws a promise instead.
88​
89## Examples
90​
91```tsx
92import { useQueries } from '@tanstack/preact-query'
93​
94function Posts({ ids }: { ids: Array<number> }) {
95 const postQueries = useQueries({
96 queries: ids.map((id) => ({
97 queryKey: ['post', id],
98 queryFn: () => fetchPost(id),
99 staleTime: Infinity,
100 })),
101 })
102​
103 return (
104 <ul>
105 {postQueries.map((query, index) => {
106 if (query.isPending) return <li key={ids[index]}>Loading...</li>
107 if (query.isError) return <li key={ids[index]}>Error: {query.error.message}</li>
108 return <li key={ids[index]}>{query.data.title}</li>
109 })}
110 </ul>
111 )
113```
114​
115Combining results into a single value:
116```tsx
117import { useQueries } from '@tanstack/preact-query'
118​
119function Posts({ ids }: { ids: Array<number> }) {
120 const { data, isPending, isError } = useQueries({
121 queries: ids.map((id) => ({
122 queryKey: ['post', id],
123 queryFn: () => fetchPost(id),
124 })),
125 combine: (postQueries) => {
126 return {
127 data: postQueries.map((query) => query.data),
128 isPending: postQueries.some((query) => query.isPending),
129 isError: postQueries.some((query) => query.isError),
130 }
131 },
132 })
133​
134 if (isPending) return 'Loading...'
135 if (isError) return 'Error loading posts'
136​
137 return (
138 <ul>
139 {data.map((post) => (
140 <li key={post?.id}>{post?.title}</li>
141 ))}
142 </ul>
143 )
145```