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