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