Open History to see the last commit
1---
2id: useLiveQuery
3title: useLiveQuery
4---
5
6## Call Signature
7
8```ts
9function useLiveQuery<TContext>(queryFn): QueryResult<TContext>;
10```
11
12Defined in: [useLiveQuery.ts:153](https://github.com/TanStack/db/blob/main/packages/preact-db/src/useLiveQuery.ts#L153)
13
14Subscribe a Preact component to a live query.
15
16The hook derives the query's identity from its structured IR on each render,
17so values captured by the callback recompile the query when they change. A
18query whose IR cannot be hashed, such as one using `fn.where`, throws
19`UnhashableQueryIRError` unless its config supplies `queryKey`. While the
20hook is mounted, each Collection ID its queries read must keep naming one
21source object, or it throws `SourceCollectionIdReuseError`.
22Return `undefined` or `null` from the callback to disable the query.
23
24### Type Parameters
25
26#### TContext
27
28`TContext` *extends* `Context`
29
30### Parameters
31
32#### queryFn
33
34(`q`) => `QueryBuilder`\<`TContext`\>
35
36### Returns
37
38`QueryResult`\<`TContext`\>
39
40### Examples
41
42```ts
43const { data, isLoading } = useLiveQuery((q) =>
44 q
45 .from({ todos: todosCollection })
46 .where(({ todos }) => eq(todos.completed, false)),
47)
48```
49
50```ts
51// Disable the query until its input exists
52const { data, isEnabled } = useLiveQuery((q) =>
53 userId
54 ? q
55 .from({ todos: todosCollection })
56 .where(({ todos }) => eq(todos.userId, userId))
57 : undefined,
58)
59```
60
61## Call Signature
62
63```ts
64function useLiveQuery<TContext>(queryFn): ConditionalQueryResult<TContext>;
65```
66
67Defined in: [useLiveQuery.ts:157](https://github.com/TanStack/db/blob/main/packages/preact-db/src/useLiveQuery.ts#L157)
68
69Subscribe a Preact component to a live query.
70
71The hook derives the query's identity from its structured IR on each render,
72so values captured by the callback recompile the query when they change. A
73query whose IR cannot be hashed, such as one using `fn.where`, throws
74`UnhashableQueryIRError` unless its config supplies `queryKey`. While the
75hook is mounted, each Collection ID its queries read must keep naming one
76source object, or it throws `SourceCollectionIdReuseError`.
77Return `undefined` or `null` from the callback to disable the query.
78
79### Type Parameters
80
81#### TContext
82
83`TContext` *extends* `Context`
84
85### Parameters
86
87#### queryFn
88
89(`q`) => `QueryBuilder`\<`TContext`\> \| `null` \| `undefined`
90
91### Returns
92
93`ConditionalQueryResult`\<`TContext`\>
94
95### Examples
96
97```ts
98const { data, isLoading } = useLiveQuery((q) =>
99 q
100 .from({ todos: todosCollection })
101 .where(({ todos }) => eq(todos.completed, false)),
102)
103```
104
105```ts
106// Disable the query until its input exists
107const { data, isEnabled } = useLiveQuery((q) =>
108 userId
109 ? q
110 .from({ todos: todosCollection })
111 .where(({ todos }) => eq(todos.userId, userId))
112 : undefined,
113)
114```
115
116## Call Signature
117
118```ts
119function useLiveQuery<TContext>(queryFn): ConditionalQueryResult<TContext>;
120```
121
122Defined in: [useLiveQuery.ts:163](https://github.com/TanStack/db/blob/main/packages/preact-db/src/useLiveQuery.ts#L163)
123
124Subscribe a Preact component to a live query.
125
126The hook derives the query's identity from its structured IR on each render,
127so values captured by the callback recompile the query when they change. A
128query whose IR cannot be hashed, such as one using `fn.where`, throws
129`UnhashableQueryIRError` unless its config supplies `queryKey`. While the
130hook is mounted, each Collection ID its queries read must keep naming one
131source object, or it throws `SourceCollectionIdReuseError`.
132Return `undefined` or `null` from the callback to disable the query.
133
134### Type Parameters
135
136#### TContext
137
138`TContext` *extends* `Context`
139
140### Parameters
141
142#### queryFn
143
144(`q`) =>
145 \| `LiveQueryCollectionConfig`\<`TContext`, `RootQueryResult`\<`TContext`\>\>
146 \| `null`
147 \| `undefined`
148
149### Returns
150
151`ConditionalQueryResult`\<`TContext`\>
152
153### Examples
154
155```ts
156const { data, isLoading } = useLiveQuery((q) =>
157 q
158 .from({ todos: todosCollection })
159 .where(({ todos }) => eq(todos.completed, false)),
160)
161```
162
163```ts
164// Disable the query until its input exists
165const { data, isEnabled } = useLiveQuery((q) =>
166 userId
167 ? q
168 .from({ todos: todosCollection })
169 .where(({ todos }) => eq(todos.userId, userId))
170 : undefined,
171)
172```
173
174## Call Signature
175
176```ts
177function useLiveQuery<TResult, TKey, TUtils>(queryFn): UseConditionalLiveQueryResult<TResult, TKey, TResult[], Collection<TResult, TKey, TUtils, StandardSchemaV1<unknown, unknown>, TResult>>;
178```
179
180Defined in: [useLiveQuery.ts:169](https://github.com/TanStack/db/blob/main/packages/preact-db/src/useLiveQuery.ts#L169)
181
182Subscribe a Preact component to a live query.
183
184The hook derives the query's identity from its structured IR on each render,
185so values captured by the callback recompile the query when they change. A
186query whose IR cannot be hashed, such as one using `fn.where`, throws
187`UnhashableQueryIRError` unless its config supplies `queryKey`. While the
188hook is mounted, each Collection ID its queries read must keep naming one
189source object, or it throws `SourceCollectionIdReuseError`.
190Return `undefined` or `null` from the callback to disable the query.
191
192### Type Parameters
193
194#### TResult
195
196`TResult` *extends* `object`
197
198#### TKey
199
200`TKey` *extends* `string` \| `number`
201
202#### TUtils
203
204`TUtils` *extends* `Record`\<`string`, `any`\>
205
206### Parameters
207
208#### queryFn
209
210(`q`) =>
211 \| `Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\>
212 \| `null`
213 \| `undefined`
214
215### Returns
216
217[`UseConditionalLiveQueryResult`](../interfaces/UseConditionalLiveQueryResult.md)\<`TResult`, `TKey`, `TResult`[], `Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\>\>
218
219### Examples
220
221```ts
222const { data, isLoading } = useLiveQuery((q) =>
223 q
224 .from({ todos: todosCollection })
225 .where(({ todos }) => eq(todos.completed, false)),
226)
227```
228
229```ts
230// Disable the query until its input exists
231const { data, isEnabled } = useLiveQuery((q) =>
232 userId
233 ? q
234 .from({ todos: todosCollection })
235 .where(({ todos }) => eq(todos.userId, userId))
236 : undefined,
237)
238```
239
240## Call Signature
241
242```ts
243function useLiveQuery<TContext>(config): QueryResult<TContext>;
244```
245
246Defined in: [useLiveQuery.ts:193](https://github.com/TanStack/db/blob/main/packages/preact-db/src/useLiveQuery.ts#L193)
247
248Subscribe to a live query described by a config object.
249
250### Type Parameters
251
252#### TContext
253
254`TContext` *extends* `Context`
255
256### Parameters
257
258#### config
259
260[`UseLiveQueryConfig`](../type-aliases/UseLiveQueryConfig.md)\<`TContext`\>
261
262### Returns
263
264`QueryResult`\<`TContext`\>
265
266### Example
267
268```ts
269const { data } = useLiveQuery({
270 query: (q) => q.from({ todos: todosCollection }),
271 gcTime: 60_000,
272})
273```
274
275## Call Signature
276
277```ts
278function useLiveQuery<TContext>(config): ConditionalQueryResult<TContext>;
279```
280
281Defined in: [useLiveQuery.ts:197](https://github.com/TanStack/db/blob/main/packages/preact-db/src/useLiveQuery.ts#L197)
282
283Subscribe a Preact component to a live query.
284
285The hook derives the query's identity from its structured IR on each render,
286so values captured by the callback recompile the query when they change. A
287query whose IR cannot be hashed, such as one using `fn.where`, throws
288`UnhashableQueryIRError` unless its config supplies `queryKey`. While the
289hook is mounted, each Collection ID its queries read must keep naming one
290source object, or it throws `SourceCollectionIdReuseError`.
291Return `undefined` or `null` from the callback to disable the query.
292
293### Type Parameters
294
295#### TContext
296
297`TContext` *extends* `Context`
298
299### Parameters
300
301#### config
302
303[`ConditionalUseLiveQueryConfig`](../type-aliases/ConditionalUseLiveQueryConfig.md)\<`TContext`\>
304
305### Returns
306
307`ConditionalQueryResult`\<`TContext`\>
308
309### Examples
310
311```ts
312const { data, isLoading } = useLiveQuery((q) =>
313 q
314 .from({ todos: todosCollection })
315 .where(({ todos }) => eq(todos.completed, false)),
316)
317```
318
319```ts
320// Disable the query until its input exists
321const { data, isEnabled } = useLiveQuery((q) =>
322 userId
323 ? q
324 .from({ todos: todosCollection })
325 .where(({ todos }) => eq(todos.userId, userId))
326 : undefined,
327)
328```
329
330## Call Signature
331
332```ts
333function useLiveQuery<TResult, TKey, TUtils>(liveQueryCollection): UseLiveQueryResult<TResult, TKey, TResult[], Collection<TResult, TKey, TUtils, StandardSchemaV1<unknown, unknown>, TResult>>;
334```
335
336Defined in: [useLiveQuery.ts:211](https://github.com/TanStack/db/blob/main/packages/preact-db/src/useLiveQuery.ts#L211)
337
338Subscribe to an existing live-query Collection. The hook starts its sync and
339leaves its retention policy to the caller.
340
341### Type Parameters
342
343#### TResult
344
345`TResult` *extends* `object`
346
347#### TKey
348
349`TKey` *extends* `string` \| `number`
350
351#### TUtils
352
353`TUtils` *extends* `Record`\<`string`, `any`\>
354
355### Parameters
356
357#### liveQueryCollection
358
359`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `NonSingleResult`
360
361### Returns
362
363[`UseLiveQueryResult`](../interfaces/UseLiveQueryResult.md)\<`TResult`, `TKey`, `TResult`[], `Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\>\>
364
365### Example
366
367```ts
368const activeTodos = createLiveQueryCollection((q) =>
369 q.from({ todos: todosCollection }).where(({ todos }) => todos.active),
370)
371const { data, collection } = useLiveQuery(activeTodos)
372```
373
374## Call Signature
375
376```ts
377function useLiveQuery<TResult, TKey, TUtils>(liveQueryCollection): UseLiveQueryResult<TResult, TKey, TResult | undefined, Collection<TResult, TKey, TUtils, StandardSchemaV1<unknown, unknown>, TResult> & SingleResult>;
378```
379
380Defined in: [useLiveQuery.ts:224](https://github.com/TanStack/db/blob/main/packages/preact-db/src/useLiveQuery.ts#L224)
381
382Subscribe a Preact component to a live query.
383
384The hook derives the query's identity from its structured IR on each render,
385so values captured by the callback recompile the query when they change. A
386query whose IR cannot be hashed, such as one using `fn.where`, throws
387`UnhashableQueryIRError` unless its config supplies `queryKey`. While the
388hook is mounted, each Collection ID its queries read must keep naming one
389source object, or it throws `SourceCollectionIdReuseError`.
390Return `undefined` or `null` from the callback to disable the query.
391
392### Type Parameters
393
394#### TResult
395
396`TResult` *extends* `object`
397
398#### TKey
399
400`TKey` *extends* `string` \| `number`
401
402#### TUtils
403
404`TUtils` *extends* `Record`\<`string`, `any`\>
405
406### Parameters
407
408#### liveQueryCollection
409
410`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `SingleResult`
411
412### Returns
413
414[`UseLiveQueryResult`](../interfaces/UseLiveQueryResult.md)\<`TResult`, `TKey`, `TResult` \| `undefined`, `Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `SingleResult`\>
415
416### Examples
417
418```ts
419const { data, isLoading } = useLiveQuery((q) =>
420 q
421 .from({ todos: todosCollection })
422 .where(({ todos }) => eq(todos.completed, false)),
423)
424```
425
426```ts
427// Disable the query until its input exists
428const { data, isEnabled } = useLiveQuery((q) =>
429 userId
430 ? q
431 .from({ todos: todosCollection })
432 .where(({ todos }) => eq(todos.userId, userId))
433 : undefined,
434)
435```