Open History to see the last commit
1---
2id: useLiveInfiniteQuery
3title: useLiveInfiniteQuery
4---
5
6# Function: useLiveInfiniteQuery()
7
8## Call Signature
9
10```ts
11function useLiveInfiniteQuery<TResult, TKey, TUtils>(liveQueryCollection, config): UseLiveInfiniteQueryReturn<any>;
12```
13
14Defined in: [useLiveInfiniteQuery.ts:118](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveInfiniteQuery.ts#L118)
15
16Create an infinite query using a query function with live updates
17
18Uses `utils.setWindow()` to dynamically adjust the limit/offset window
19without recreating the live query collection on each page change.
20
21### Type Parameters
22
23#### TResult
24
25`TResult` *extends* `object`
26
27#### TKey
28
29`TKey` *extends* `string` \| `number`
30
31#### TUtils
32
33`TUtils` *extends* `Record`\<`string`, `any`\>
34
35### Parameters
36
37#### liveQueryCollection
38
39`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `NonSingleResult`
40
41#### config
42
43[`UseLiveInfiniteQueryConfig`](../type-aliases/UseLiveInfiniteQueryConfig.md)\<`any`\>
44
45Configuration including pageSize and getNextPageParam
46
47### Returns
48
49[`UseLiveInfiniteQueryReturn`](../type-aliases/UseLiveInfiniteQueryReturn.md)\<`any`\>
50
51Object with pages, data, and pagination controls
52
53### Examples
54
55```ts
56// Basic infinite query
57const { data, pages, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(
58 (q) => q
59 .from({ posts: postsCollection })
60 .orderBy(({ posts }) => posts.createdAt, 'desc')
61 .select(({ posts }) => ({
62 id: posts.id,
63 title: posts.title
64 })),
65 {
66 pageSize: 20,
67 getNextPageParam: (lastPage, allPages) =>
68 lastPage.length === 20 ? allPages.length : undefined
69 }
70)
71```
72
73```ts
74// With dependencies
75const { pages, fetchNextPage } = useLiveInfiniteQuery(
76 (q) => q
77 .from({ posts: postsCollection })
78 .where(({ posts }) => eq(posts.category, category))
79 .orderBy(({ posts }) => posts.createdAt, 'desc'),
80 {
81 pageSize: 10,
82 getNextPageParam: (lastPage) =>
83 lastPage.length === 10 ? lastPage.length : undefined
84 },
85 [category]
86)
87```
88
89```ts
90// Router loader pattern with pre-created collection
91// In loader:
92const postsQuery = createLiveQueryCollection({
93 query: (q) => q
94 .from({ posts: postsCollection })
95 .orderBy(({ posts }) => posts.createdAt, 'desc')
96 .limit(20)
97})
98await postsQuery.preload()
99return { postsQuery }
100
101// In component:
102const { postsQuery } = useLoaderData()
103const { data, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(
104 postsQuery,
105 {
106 pageSize: 20,
107 getNextPageParam: (lastPage) => lastPage.length === 20 ? lastPage.length : undefined
108 }
109)
110```
111
112## Call Signature
113
114```ts
115function useLiveInfiniteQuery<TContext>(
116 queryFn,
117 config,
118deps?): UseLiveInfiniteQueryReturn<TContext>;
119```
120
121Defined in: [useLiveInfiniteQuery.ts:128](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveInfiniteQuery.ts#L128)
122
123Create an infinite query using a query function with live updates
124
125Uses `utils.setWindow()` to dynamically adjust the limit/offset window
126without recreating the live query collection on each page change.
127
128### Type Parameters
129
130#### TContext
131
132`TContext` *extends* `Context`
133
134### Parameters
135
136#### queryFn
137
138(`q`) => `QueryBuilder`\<`TContext`\>
139
140Query function that defines what data to fetch. Must include `.orderBy()` for setWindow to work.
141
142#### config
143
144[`UseLiveInfiniteQueryConfig`](../type-aliases/UseLiveInfiniteQueryConfig.md)\<`TContext`\>
145
146Configuration including pageSize and getNextPageParam
147
148#### deps?
149
150`unknown`[]
151
152Array of dependencies that trigger query re-execution when changed
153
154### Returns
155
156[`UseLiveInfiniteQueryReturn`](../type-aliases/UseLiveInfiniteQueryReturn.md)\<`TContext`\>
157
158Object with pages, data, and pagination controls
159
160### Examples
161
162```ts
163// Basic infinite query
164const { data, pages, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(
165 (q) => q
166 .from({ posts: postsCollection })
167 .orderBy(({ posts }) => posts.createdAt, 'desc')
168 .select(({ posts }) => ({
169 id: posts.id,
170 title: posts.title
171 })),
172 {
173 pageSize: 20,
174 getNextPageParam: (lastPage, allPages) =>
175 lastPage.length === 20 ? allPages.length : undefined
176 }
177)
178```
179
180```ts
181// With dependencies
182const { pages, fetchNextPage } = useLiveInfiniteQuery(
183 (q) => q
184 .from({ posts: postsCollection })
185 .where(({ posts }) => eq(posts.category, category))
186 .orderBy(({ posts }) => posts.createdAt, 'desc'),
187 {
188 pageSize: 10,
189 getNextPageParam: (lastPage) =>
190 lastPage.length === 10 ? lastPage.length : undefined
191 },
192 [category]
193)
194```
195
196```ts
197// Router loader pattern with pre-created collection
198// In loader:
199const postsQuery = createLiveQueryCollection({
200 query: (q) => q
201 .from({ posts: postsCollection })
202 .orderBy(({ posts }) => posts.createdAt, 'desc')
203 .limit(20)
204})
205await postsQuery.preload()
206return { postsQuery }
207
208// In component:
209const { postsQuery } = useLoaderData()
210const { data, fetchNextPage, hasNextPage } = useLiveInfiniteQuery(
211 postsQuery,
212 {
213 pageSize: 20,
214 getNextPageParam: (lastPage) => lastPage.length === 20 ? lastPage.length : undefined
215 }
216)
217```