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

useQueries.md

160 lines5.1 KB

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```