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

createQueriesController.md

97 lines2.7 KB

1---
2id: createQueriesController
3title: createQueriesController
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8```ts
9function createQueriesController<TQueryOptions extends any[], TCombinedResult = CreateQueriesResults<TQueryOptions>>(
10 host: ReactiveControllerHost,
11 options: Accessor<CreateQueriesControllerOptions<TQueryOptions, TCombinedResult>>,
12 queryClient?: QueryClient
13): QueriesResultAccessor<TCombinedResult>;
14```
15​
16Defined in: [packages/lit-query/src/createQueriesController.ts:711](https://github.com/TanStack/query/blob/main/packages/lit-query/src/createQueriesController.ts#L711)
17​
18Creates a Lit reactive controller that subscribes the host to multiple
19queries.
20​
21The returned accessor is callable and also exposes `current` and `destroy`.
22When `options` or `options.queries` is a function, it is re-read during host
23updates so the query list can follow reactive host state.
24​
25If `queryClient` is omitted, the controller resolves the client from the
26nearest connected `QueryClientProvider`.
27​
28## Type Parameters
29​
30### TQueryOptions
31​
32`TQueryOptions` *extends* `any`[]
33​
34### TCombinedResult
35​
36`TCombinedResult` = `CreateQueriesResults`\<`TQueryOptions`\>
37​
38## Parameters
39​
40### host
41​
42`ReactiveControllerHost`
43​
44The Lit reactive controller host that owns the queries
45subscription.
46​
47### options
48​
49[`Accessor`](../type-aliases/Accessor.md)\<[`CreateQueriesControllerOptions`](../type-aliases/CreateQueriesControllerOptions.md)\<`TQueryOptions`, `TCombinedResult`\>\>
50​
51Queries controller options, or a getter that returns options.
52​
53<a id="options-properties"></a>
54​
55#### `options` properties
56​
57Built from [`CreateQueriesControllerOptions`](../type-aliases/CreateQueriesControllerOptions.md#properties). See the type above for what it changes.
58​
59### queryClient?
60​
61[`QueryClient`](../classes/QueryClient.md)
62​
63Optional explicit query client. Provide this for
64controllers that should not resolve a client from Lit context.
65​
66## Returns
67​
68[`QueriesResultAccessor`](../type-aliases/QueriesResultAccessor.md)\<`TCombinedResult`\>
69​
70An accessor for the latest query results, or the value returned by
71`combine`.
72​
73## Example
74​
75```ts
76import { LitElement, html } from 'lit'
77import { createQueriesController } from '@tanstack/lit-query'
78​
79class DashboardView extends LitElement {
80 private readonly dashboard = createQueriesController(this, {
81 queries: [
82 { queryKey: ['stats'], queryFn: fetchStats },
83 { queryKey: ['projects'], queryFn: fetchProjects },
84 ],
85 combine: ([stats, projects]) => ({
86 stats: stats.data,
87 projects: projects.data ?? [],
88 isPending: stats.isPending || projects.isPending,
89 }),
90 })
91​
92 render() {
93 const dashboard = this.dashboard()
94 return html`<p>Projects: ${dashboard.projects.length}</p>`
95 }
97```