Open History to see the last commit
1---
2id: useQueries
3title: useQueries
4redirect_from:
5 - framework/vue/reference/useQueries
6---
7β
8<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
9β
10```ts
11function useQueries<T extends any[], 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]> }>(options: ShallowOption & object, queryClient?: QueryClient): Readonly<Ref<TCombinedResult>>;
12```
13β
14Defined in: [packages/vue-query/src/useQueries.ts:354](https://github.com/TanStack/query/blob/main/packages/vue-query/src/useQueries.ts#L354)
15β
16The `useQueries` composable can be used to fetch a variable number of queries.
17β
18The `queries` option accepts an array of query option objects mostly identical to `useQuery`'s. It may be a
19plain array, a `ref`/reactive array (each entry tracked individually), or a getter function β pass a getter
20if the array itself (its length, or which queries are in it) depends on other reactive state.
21β
22Having the same query key more than once in the array of query objects may cause some data to be shared
23between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
24structure.
25β
26The `combine` option can be used to combine the results of the queries into a single value. The result will
27be structurally shared to be as referentially stable as possible.
28β
29`placeholderData` is supported here too, but unlike `useQuery`, it doesn't receive information from
30previously rendered queries, because the number of queries can differ between renders.
31β
32## Type Parameters
33β
34### T
35β
36`T` *extends* `any`[]
37β
38### TCombinedResult
39β
40`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\]\> \}
41β
42## Parameters
43β
44### options
45β
46`ShallowOption` & `object`
47β
48The `queries` array to run, and the optional `combine` and `shallow` options.
49β
50### queryClient?
51β
52[`QueryClient`](../classes/QueryClient.md)
53β
54Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
55will be used.
56β
57## Returns
58β
59`Readonly`\<`Ref`\<`TCombinedResult`\>\>
60β
61A `ref` to the combined result. Without `combine`, this is an array with all the query results, in
62the same order as the input. When `combine` is provided, this is the value returned by `combine` instead.
63β
64## Examples
65β
66```vue
67<script setup lang="ts">
68import { useQueries } from '@tanstack/vue-query'
69β
70const props = defineProps<{ ids: Array<number> }>()
71β
72const postQueries = useQueries({
73 queries: () =>
74 props.ids.map((id) => ({
75 queryKey: ['post', id],
76 queryFn: () => fetchPost(id),
77 staleTime: Infinity,
78 })),
79})
80</script>
81β
82<template>
83 <ul>
84 <li v-for="(query, index) in postQueries" :key="props.ids[index]">
85 <span v-if="query.isPending">Loading...</span>
86 <span v-else-if="query.isError">Error: {{ query.error.message }}</span>
87 <span v-else>{{ query.data.title }}</span>
88 </li>
89 </ul>
90</template>
91```
92β
93Combining results into a single value:
94```vue
95<script setup lang="ts">
96import { useQueries } from '@tanstack/vue-query'
97β
98const props = defineProps<{ ids: Array<number> }>()
99β
100const combined = useQueries({
101 queries: () =>
102 props.ids.map((id) => ({
103 queryKey: ['post', id],
104 queryFn: () => fetchPost(id),
105 })),
106 combine: (postQueries) => ({
107 data: postQueries.map((query) => query.data),
108 isPending: postQueries.some((query) => query.isPending),
109 isError: postQueries.some((query) => query.isError),
110 }),
111})
112</script>
113β
114<template>
115 <span v-if="combined.isPending">Loading...</span>
116 <span v-else-if="combined.isError">Error!</span>
117 <ul v-else>
118 <li v-for="post in combined.data" :key="post.id">{{ post.title }}</li>
119 </ul>
120</template>
121```
122β
123Typing `select` via [queryOptions](queryOptions.md). Note that spreading a `queryOptions` result and overriding
124`select` inline still falls back to `unknown` β wrap the spread in `queryOptions` again so the override is
125resolved before it reaches `useQueries`:
126```vue
127<script setup lang="ts">
128import { queryOptions, useQueries } from '@tanstack/vue-query'
129β
130const props = defineProps<{ id: number }>()
131β
132const postOptions = (id: number) =>
133 queryOptions({
134 queryKey: ['post', id],
135 queryFn: () => fetchPost(id),
136 })
137β
138const [{ data: broken }] = useQueries({
139 queries: () => [
140 {
141 ...postOptions(props.id),
142 // β `data` is `unknown` here
143 select: (data) => data.title,
144 },
145 ],
146})
147β
148const [{ data: fixed }] = useQueries({
149 queries: () => [
150 queryOptions({
151 ...postOptions(props.id),
152 // β
`data` is `Post`
153 select: (data) => data.title,
154 }),
155 ],
156})
157</script>
158β
159<template>
160 <h1>{{ fixed }}</h1>
161</template>
162```