Open History to see the last commit
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 )
112}
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 )
144}
145```