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

createQueriesController.md

88 lines2.2 KB

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