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