Open History to see the last commit
1---
2id: createInfiniteQueryController
3title: createInfiniteQueryController
4---
5β
6# Function: createInfiniteQueryController()
7β
8```ts
9function createInfiniteQueryController<TQueryFnData, TError, TData, TQueryKey, TPageParam>(
10 host,
11 options,
12queryClient?): InfiniteQueryResultAccessor<TData, TError>;
13```
14β
15Defined in: [packages/lit-query/src/createInfiniteQueryController.ts:364](https://github.com/TanStack/query/blob/main/packages/lit-query/src/createInfiniteQueryController.ts#L364)
16β
17Creates a Lit reactive controller that subscribes the host to an infinite
18query.
19β
20The returned accessor is callable and also exposes `current`, `refetch`,
21`fetchNextPage`, `fetchPreviousPage`, and `destroy`. When `options` is a
22function, it is re-read during host updates so query keys and options can
23follow reactive host state.
24β
25If `queryClient` is omitted, the controller resolves the client from the
26nearest connected `QueryClientProvider`.
27β
28## Type Parameters
29β
30### TQueryFnData
31β
32`TQueryFnData` = `unknown`
33β
34### TError
35β
36`TError` = `Error`
37β
38### TData
39β
40`TData` = `InfiniteData`\<`TQueryFnData`, `unknown`\>
41β
42### TQueryKey
43β
44`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
45β
46### TPageParam
47β
48`TPageParam` = `unknown`
49β
50## Parameters
51β
52### host
53β
54`ReactiveControllerHost`
55β
56The Lit reactive controller host that owns the infinite query
57subscription.
58β
59### options
60β
61[`Accessor`](../type-aliases/Accessor.md)\<[`CreateInfiniteQueryOptions`](../type-aliases/CreateInfiniteQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>\>
62β
63Infinite query observer options, or a getter that returns
64options.
65β
66### queryClient?
67β
68`QueryClient`
69β
70Optional explicit query client. Provide this for
71controllers that should not resolve a client from Lit context.
72β
73## Returns
74β
75[`InfiniteQueryResultAccessor`](../type-aliases/InfiniteQueryResultAccessor.md)\<`TData`, `TError`\>
76β
77An accessor for the latest infinite query result with page helper
78methods.
79β
80## Example
81β
82```ts
83import { LitElement, html } from 'lit'
84import { createInfiniteQueryController } from '@tanstack/lit-query'
85β
86class ProjectsView extends LitElement {
87 private readonly projects = createInfiniteQueryController(this, {
88 queryKey: ['projects'],
89 queryFn: ({ pageParam }) => fetchProjects(pageParam),
90 initialPageParam: 0,
91 getNextPageParam: (lastPage) => lastPage.nextCursor,
92 })
93β
94 render() {
95 const query = this.projects()
96β
97 return html`
98 <button ?disabled=${!query.hasNextPage} @click=${() => this.projects.fetchNextPage()}>
99 Load more
100 </button>
101 `
102 }
103}
104```