Files
upstream-tanstack-query/docs/framework/lit/reference/functions/createQueryController.md
upstream-mirror 3a013ccdfe
Some checks failed
autofix.ci / autofix (push) Failing after 4s
Release / Release (push) Has been skipped
GitHub Actions Security Analysis / Run zizmor (push) Failing after 2s
snapshot of https://github.com/TanStack/query.git (main, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:13:08 +05:00

96 lines
2.3 KiB
Markdown

---
id: createQueryController
title: createQueryController
---
```ts
function createQueryController<TQueryFnData, TError, TData, TQueryData, TQueryKey>(
host,
options,
queryClient?): QueryResultAccessor<TData, TError>;
```
Defined in: [packages/lit-query/src/createQueryController.ts:355](https://github.com/TanStack/query/blob/main/packages/lit-query/src/createQueryController.ts#L355)
Creates a Lit reactive controller that subscribes the host to a single query.
The returned accessor is callable and also exposes `current`, `refetch`,
`suspense`, and `destroy`. When `options` is a function, it is re-read during
host updates so query keys and options can follow reactive host state.
If `queryClient` is omitted, the controller resolves the client from the
nearest connected `QueryClientProvider`.
## Type Parameters
### TQueryFnData
`TQueryFnData` = `unknown`
### TError
`TError` = `Error`
### TData
`TData` = `TQueryFnData`
### TQueryData
`TQueryData` = `TQueryFnData`
### TQueryKey
`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
## Parameters
### host
`ReactiveControllerHost`
The Lit reactive controller host that owns the query
subscription.
### options
[`Accessor`](../type-aliases/Accessor.md)\<[`CreateQueryOptions`](../type-aliases/CreateQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryData`, `TQueryKey`\>\>
Query observer options, or a getter that returns options.
### queryClient?
[`QueryClient`](../classes/QueryClient.md)
Optional explicit query client. Provide this for
controllers that should not resolve a client from Lit context.
## Returns
[`QueryResultAccessor`](../type-aliases/QueryResultAccessor.md)\<`TData`, `TError`\>
An accessor for the latest query result with query helper methods.
## Example
```ts
import { LitElement, html } from 'lit'
import { createQueryController } from '@tanstack/lit-query'
class TodosView extends LitElement {
private readonly todos = createQueryController(this, {
queryKey: ['todos'],
queryFn: async () => fetch('/api/todos').then((r) => r.json()),
})
render() {
const query = this.todos()
if (query.isPending) return html`Loading...`
if (query.isError) return html`Error`
return html`<ul>${query.data.map((todo) => html`<li>${todo.title}</li>`)}</ul>`
}
}
```