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

createQueriesController.md

90 lines2.3 KB

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