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

useQueries.md

162 lines5.8 KB

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:353](https://github.com/TanStack/query/blob/main/packages/vue-query/src/useQueries.ts#L353)
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```