Open History to see the last commit
1---
2id: createQueryController
3title: createQueryController
4---
5β
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7β
8```ts
9function createQueryController<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryData = TQueryFnData, TQueryKey extends readonly unknown[] = readonly unknown[]>(
10 host: ReactiveControllerHost,
11 options: Accessor<CreateQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>>,
12 queryClient?: QueryClient
13): QueryResultAccessor<TData, TError>;
14```
15β
16Defined in: [packages/lit-query/src/createQueryController.ts:348](https://github.com/TanStack/query/blob/main/packages/lit-query/src/createQueryController.ts#L348)
17β
18Creates a Lit reactive controller that subscribes the host to a single query.
19β
20The returned accessor is callable and also exposes `current`, `refetch`,
21`suspense`, and `destroy`. When `options` is a function, it is re-read during
22host updates so query keys and options 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### TQueryFnData
30β
31`TQueryFnData` = `unknown`
32β
33### TError
34β
35`TError` = `Error`
36β
37### TData
38β
39`TData` = `TQueryFnData`
40β
41### TQueryData
42β
43`TQueryData` = `TQueryFnData`
44β
45### TQueryKey
46β
47`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
48β
49## Parameters
50β
51### host
52β
53`ReactiveControllerHost`
54β
55The Lit reactive controller host that owns the query
56subscription.
57β
58### options
59β
60[`Accessor`](../type-aliases/Accessor.md)\<[`CreateQueryOptions`](../type-aliases/CreateQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryData`, `TQueryKey`\>\>
61β
62Query observer options, or a getter that returns options.
63β
64<a id="options-properties"></a>
65β
66#### `options` properties
67β
68Built from [`QueryObserverOptions`](../interfaces/QueryObserverOptions.md#properties). See the type above for what it changes.
69β
70### queryClient?
71β
72[`QueryClient`](../classes/QueryClient.md)
73β
74Optional explicit query client. Provide this for
75controllers that should not resolve a client from Lit context.
76β
77## Returns
78β
79[`QueryResultAccessor`](../type-aliases/QueryResultAccessor.md)\<`TData`, `TError`\>
80β
81An accessor for the latest query result with query helper methods.
82β
83## Example
84β
85```ts
86import { LitElement, html } from 'lit'
87import { createQueryController } from '@tanstack/lit-query'
88β
89class TodosView extends LitElement {
90 private readonly todos = createQueryController(this, {
91 queryKey: ['todos'],
92 queryFn: async () => fetch('/api/todos').then((r) => r.json()),
93 })
94β
95 render() {
96 const query = this.todos()
97β
98 if (query.isPending) return html`Loading...`
99 if (query.isError) return html`Error`
100β
101 return html`<ul>${query.data.map((todo) => html`<li>${todo.title}</li>`)}</ul>`
102 }
103}
104```