Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

useLiveQuery.md

1,106 lines24.0 KB

1---
2id: useLiveQuery
3title: useLiveQuery
4---
5​
6## Call Signature
7​
8```ts
9function useLiveQuery<TContext>(queryFn, deps?): object & LiveQueryStatusFlags & object;
10```
11​
12Defined in: [useLiveQuery.ts:394](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveQuery.ts#L394)
13​
14Create a live query using a query function.
15​
16### Type Parameters
17​
18#### TContext
19​
20`TContext` *extends* `Context`
21​
22### Parameters
23​
24#### queryFn
25​
26(`q`) => `QueryBuilder`\<`TContext`\>
27​
28Query function that defines what data to fetch
29​
30#### deps?
31​
32`unknown`[]
33​
34Deprecated array of dependencies that trigger query re-execution when changed
35​
36### Returns
37​
38Object with reactive data, state, and status information
39​
40### Examples
41​
42```ts
43// Prefer config object syntax
44const { data, isLoading } = useLiveQuery({
45 query: (q) =>
46 q.from({ todos: todosCollection })
47 .where(({ todos }) => eq(todos.completed, false))
48 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
49})
50```
51​
52```ts
53// Single result query
54const { data } = useLiveQuery({
55 query: (q) => q.from({ todos: todosCollection })
56 .where(({ todos }) => eq(todos.id, 1))
57 .findOne()
58})
59```
60​
61```ts
62// Structured captured values are included in derived query identity
63const { data, state } = useLiveQuery({
64 query: (q) => q.from({ todos: todosCollection })
65 .where(({ todos }) => gt(todos.priority, minPriority)),
66})
67```
68​
69```ts
70// Return undefined or null to disable a query
71const { data, isEnabled } = useLiveQuery({
72 query: (q) => {
73 if (!userId) return undefined
74 return q.from({ todos: todosCollection })
75 .where(({ todos }) => eq(todos.userId, userId))
76 },
77})
78```
79​
80```ts
81// Join pattern
82const { data } = useLiveQuery({
83 query: (q) =>
84 q.from({ issues: issueCollection })
85 .join({ persons: personCollection }, ({ issues, persons }) =>
86 eq(issues.userId, persons.id)
87 )
88 .select(({ issues, persons }) => ({
89 id: issues.id,
90 title: issues.title,
91 userName: persons.name
92 }))
93})
94```
95​
96```ts
97// Handle loading and error states
98const { data, isLoading, isLoadingError, status } = useLiveQuery({
99 query: (q) => q.from({ todos: todoCollection })
100})
101​
102if (isLoading) return <div>Loading...</div>
103if (isLoadingError) return <div>Error: {status}</div>
104​
105return (
106 <ul>
107 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
108 </ul>
110```
111​
112## Call Signature
113​
114```ts
115function useLiveQuery<TContext>(queryFn, deps?): object & LiveQueryStatusFlags & object;
116```
117​
118Defined in: [useLiveQuery.ts:406](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveQuery.ts#L406)
119​
120Create a live query using a query function.
121​
122### Type Parameters
123​
124#### TContext
125​
126`TContext` *extends* `Context`
127​
128### Parameters
129​
130#### queryFn
131​
132(`q`) => `QueryBuilder`\<`TContext`\> \| `null` \| `undefined`
133​
134Query function that defines what data to fetch
135​
136#### deps?
137​
138`unknown`[]
139​
140Deprecated array of dependencies that trigger query re-execution when changed
141​
142### Returns
143​
144Object with reactive data, state, and status information
145​
146### Examples
147​
148```ts
149// Prefer config object syntax
150const { data, isLoading } = useLiveQuery({
151 query: (q) =>
152 q.from({ todos: todosCollection })
153 .where(({ todos }) => eq(todos.completed, false))
154 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
155})
156```
157​
158```ts
159// Single result query
160const { data } = useLiveQuery({
161 query: (q) => q.from({ todos: todosCollection })
162 .where(({ todos }) => eq(todos.id, 1))
163 .findOne()
164})
165```
166​
167```ts
168// Structured captured values are included in derived query identity
169const { data, state } = useLiveQuery({
170 query: (q) => q.from({ todos: todosCollection })
171 .where(({ todos }) => gt(todos.priority, minPriority)),
172})
173```
174​
175```ts
176// Return undefined or null to disable a query
177const { data, isEnabled } = useLiveQuery({
178 query: (q) => {
179 if (!userId) return undefined
180 return q.from({ todos: todosCollection })
181 .where(({ todos }) => eq(todos.userId, userId))
182 },
183})
184```
185​
186```ts
187// Join pattern
188const { data } = useLiveQuery({
189 query: (q) =>
190 q.from({ issues: issueCollection })
191 .join({ persons: personCollection }, ({ issues, persons }) =>
192 eq(issues.userId, persons.id)
193 )
194 .select(({ issues, persons }) => ({
195 id: issues.id,
196 title: issues.title,
197 userName: persons.name
198 }))
199})
200```
201​
202```ts
203// Handle loading and error states
204const { data, isLoading, isLoadingError, status } = useLiveQuery({
205 query: (q) => q.from({ todos: todoCollection })
206})
207​
208if (isLoading) return <div>Loading...</div>
209if (isLoadingError) return <div>Error: {status}</div>
210​
211return (
212 <ul>
213 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
214 </ul>
216```
217​
218## Call Signature
219​
220```ts
221function useLiveQuery<TContext>(queryFn, deps?): object & LiveQueryStatusFlags & object;
222```
223​
224Defined in: [useLiveQuery.ts:420](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveQuery.ts#L420)
225​
226Create a live query using a query function.
227​
228### Type Parameters
229​
230#### TContext
231​
232`TContext` *extends* `Context`
233​
234### Parameters
235​
236#### queryFn
237​
238(`q`) =>
239 \| `LiveQueryCollectionConfig`\<`TContext`, `RootQueryResult`\<`TContext`\>\>
240 \| `null`
241 \| `undefined`
242​
243Query function that defines what data to fetch
244​
245#### deps?
246​
247`unknown`[]
248​
249Deprecated array of dependencies that trigger query re-execution when changed
250​
251### Returns
252​
253Object with reactive data, state, and status information
254​
255### Examples
256​
257```ts
258// Prefer config object syntax
259const { data, isLoading } = useLiveQuery({
260 query: (q) =>
261 q.from({ todos: todosCollection })
262 .where(({ todos }) => eq(todos.completed, false))
263 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
264})
265```
266​
267```ts
268// Single result query
269const { data } = useLiveQuery({
270 query: (q) => q.from({ todos: todosCollection })
271 .where(({ todos }) => eq(todos.id, 1))
272 .findOne()
273})
274```
275​
276```ts
277// Structured captured values are included in derived query identity
278const { data, state } = useLiveQuery({
279 query: (q) => q.from({ todos: todosCollection })
280 .where(({ todos }) => gt(todos.priority, minPriority)),
281})
282```
283​
284```ts
285// Return undefined or null to disable a query
286const { data, isEnabled } = useLiveQuery({
287 query: (q) => {
288 if (!userId) return undefined
289 return q.from({ todos: todosCollection })
290 .where(({ todos }) => eq(todos.userId, userId))
291 },
292})
293```
294​
295```ts
296// Join pattern
297const { data } = useLiveQuery({
298 query: (q) =>
299 q.from({ issues: issueCollection })
300 .join({ persons: personCollection }, ({ issues, persons }) =>
301 eq(issues.userId, persons.id)
302 )
303 .select(({ issues, persons }) => ({
304 id: issues.id,
305 title: issues.title,
306 userName: persons.name
307 }))
308})
309```
310​
311```ts
312// Handle loading and error states
313const { data, isLoading, isLoadingError, status } = useLiveQuery({
314 query: (q) => q.from({ todos: todoCollection })
315})
316​
317if (isLoading) return <div>Loading...</div>
318if (isLoadingError) return <div>Error: {status}</div>
319​
320return (
321 <ul>
322 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
323 </ul>
325```
326​
327## Call Signature
328​
329```ts
330function useLiveQuery<TResult, TKey, TUtils>(queryFn, deps?): object & LiveQueryStatusFlags & object;
331```
332​
333Defined in: [useLiveQuery.ts:434](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveQuery.ts#L434)
334​
335Create a live query using a query function.
336​
337### Type Parameters
338​
339#### TResult
340​
341`TResult` *extends* `object`
342​
343#### TKey
344​
345`TKey` *extends* `string` \| `number`
346​
347#### TUtils
348​
349`TUtils` *extends* `Record`\<`string`, `any`\>
350​
351### Parameters
352​
353#### queryFn
354​
355(`q`) =>
356 \| `Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\>
357 \| `null`
358 \| `undefined`
359​
360Query function that defines what data to fetch
361​
362#### deps?
363​
364`unknown`[]
365​
366Deprecated array of dependencies that trigger query re-execution when changed
367​
368### Returns
369​
370Object with reactive data, state, and status information
371​
372### Examples
373​
374```ts
375// Prefer config object syntax
376const { data, isLoading } = useLiveQuery({
377 query: (q) =>
378 q.from({ todos: todosCollection })
379 .where(({ todos }) => eq(todos.completed, false))
380 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
381})
382```
383​
384```ts
385// Single result query
386const { data } = useLiveQuery({
387 query: (q) => q.from({ todos: todosCollection })
388 .where(({ todos }) => eq(todos.id, 1))
389 .findOne()
390})
391```
392​
393```ts
394// Structured captured values are included in derived query identity
395const { data, state } = useLiveQuery({
396 query: (q) => q.from({ todos: todosCollection })
397 .where(({ todos }) => gt(todos.priority, minPriority)),
398})
399```
400​
401```ts
402// Return undefined or null to disable a query
403const { data, isEnabled } = useLiveQuery({
404 query: (q) => {
405 if (!userId) return undefined
406 return q.from({ todos: todosCollection })
407 .where(({ todos }) => eq(todos.userId, userId))
408 },
409})
410```
411​
412```ts
413// Join pattern
414const { data } = useLiveQuery({
415 query: (q) =>
416 q.from({ issues: issueCollection })
417 .join({ persons: personCollection }, ({ issues, persons }) =>
418 eq(issues.userId, persons.id)
419 )
420 .select(({ issues, persons }) => ({
421 id: issues.id,
422 title: issues.title,
423 userName: persons.name
424 }))
425})
426```
427​
428```ts
429// Handle loading and error states
430const { data, isLoading, isLoadingError, status } = useLiveQuery({
431 query: (q) => q.from({ todos: todoCollection })
432})
433​
434if (isLoading) return <div>Loading...</div>
435if (isLoadingError) return <div>Error: {status}</div>
436​
437return (
438 <ul>
439 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
440 </ul>
442```
443​
444## Call Signature
445​
446```ts
447function useLiveQuery<TContext, TResult, TKey, TUtils>(queryFn, deps?): object & LiveQueryStatusFlags & object;
448```
449​
450Defined in: [useLiveQuery.ts:452](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveQuery.ts#L452)
451​
452Create a live query using a query function.
453​
454### Type Parameters
455​
456#### TContext
457​
458`TContext` *extends* `Context`
459​
460#### TResult
461​
462`TResult` *extends* `object`
463​
464#### TKey
465​
466`TKey` *extends* `string` \| `number`
467​
468#### TUtils
469​
470`TUtils` *extends* `Record`\<`string`, `any`\>
471​
472### Parameters
473​
474#### queryFn
475​
476(`q`) =>
477 \| `QueryBuilder`\<`TContext`\>
478 \| `LiveQueryCollectionConfig`\<`TContext`, `RootQueryResult`\<`TContext`\>\>
479 \| `Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\>
480 \| `null`
481 \| `undefined`
482​
483Query function that defines what data to fetch
484​
485#### deps?
486​
487`unknown`[]
488​
489Deprecated array of dependencies that trigger query re-execution when changed
490​
491### Returns
492​
493Object with reactive data, state, and status information
494​
495### Examples
496​
497```ts
498// Prefer config object syntax
499const { data, isLoading } = useLiveQuery({
500 query: (q) =>
501 q.from({ todos: todosCollection })
502 .where(({ todos }) => eq(todos.completed, false))
503 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
504})
505```
506​
507```ts
508// Single result query
509const { data } = useLiveQuery({
510 query: (q) => q.from({ todos: todosCollection })
511 .where(({ todos }) => eq(todos.id, 1))
512 .findOne()
513})
514```
515​
516```ts
517// Structured captured values are included in derived query identity
518const { data, state } = useLiveQuery({
519 query: (q) => q.from({ todos: todosCollection })
520 .where(({ todos }) => gt(todos.priority, minPriority)),
521})
522```
523​
524```ts
525// Return undefined or null to disable a query
526const { data, isEnabled } = useLiveQuery({
527 query: (q) => {
528 if (!userId) return undefined
529 return q.from({ todos: todosCollection })
530 .where(({ todos }) => eq(todos.userId, userId))
531 },
532})
533```
534​
535```ts
536// Join pattern
537const { data } = useLiveQuery({
538 query: (q) =>
539 q.from({ issues: issueCollection })
540 .join({ persons: personCollection }, ({ issues, persons }) =>
541 eq(issues.userId, persons.id)
542 )
543 .select(({ issues, persons }) => ({
544 id: issues.id,
545 title: issues.title,
546 userName: persons.name
547 }))
548})
549```
550​
551```ts
552// Handle loading and error states
553const { data, isLoading, isLoadingError, status } = useLiveQuery({
554 query: (q) => q.from({ todos: todoCollection })
555})
556​
557if (isLoading) return <div>Loading...</div>
558if (isLoadingError) return <div>Error: {status}</div>
559​
560return (
561 <ul>
562 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
563 </ul>
565```
566​
567## Call Signature
568​
569```ts
570function useLiveQuery<TContext>(config): object & LiveQueryStatusFlags & object;
571```
572​
573Defined in: [useLiveQuery.ts:515](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveQuery.ts#L515)
574​
575Create a live query using configuration object
576​
577### Type Parameters
578​
579#### TContext
580​
581`TContext` *extends* `Context`
582​
583### Parameters
584​
585#### config
586​
587[`UseLiveQueryConfig`](../type-aliases/UseLiveQueryConfig.md)\<`TContext`\>
588​
589Configuration object with query and options
590​
591### Returns
592​
593Object with reactive data, state, and status information
594​
595### Examples
596​
597```ts
598// Basic config object usage
599const { data, status } = useLiveQuery({
600 query: (q) => q.from({ todos: todosCollection }),
601 gcTime: 60000
602})
603```
604​
605```ts
606// With query builder and options
607const queryBuilder = new Query()
608 .from({ persons: collection })
609 .where(({ persons }) => gt(persons.age, 30))
610 .select(({ persons }) => ({ id: persons.id, name: persons.name }))
611​
612const { data, isReady } = useLiveQuery({
613 query: queryBuilder,
614})
615```
616​
617```ts
618// Handle all states uniformly
619const { data, isLoading, isReady, isLoadingError } = useLiveQuery({
620 query: (q) => q.from({ items: itemCollection })
621})
622​
623if (isLoading) return <div>Loading...</div>
624if (isLoadingError) return <div>Something went wrong</div>
625if (!isReady) return <div>Preparing...</div>
626​
627return <div>{data.length} items loaded</div>
628```
629​
630## Call Signature
631​
632```ts
633function useLiveQuery<TContext>(config): object & LiveQueryStatusFlags & object;
634```
635​
636Defined in: [useLiveQuery.ts:526](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveQuery.ts#L526)
637​
638Create a live query using a query function.
639​
640### Type Parameters
641​
642#### TContext
643​
644`TContext` *extends* `Context`
645​
646### Parameters
647​
648#### config
649​
650[`ConditionalUseLiveQueryConfig`](../type-aliases/ConditionalUseLiveQueryConfig.md)\<`TContext`\>
651​
652### Returns
653​
654Object with reactive data, state, and status information
655​
656### Examples
657​
658```ts
659// Prefer config object syntax
660const { data, isLoading } = useLiveQuery({
661 query: (q) =>
662 q.from({ todos: todosCollection })
663 .where(({ todos }) => eq(todos.completed, false))
664 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
665})
666```
667​
668```ts
669// Single result query
670const { data } = useLiveQuery({
671 query: (q) => q.from({ todos: todosCollection })
672 .where(({ todos }) => eq(todos.id, 1))
673 .findOne()
674})
675```
676​
677```ts
678// Structured captured values are included in derived query identity
679const { data, state } = useLiveQuery({
680 query: (q) => q.from({ todos: todosCollection })
681 .where(({ todos }) => gt(todos.priority, minPriority)),
682})
683```
684​
685```ts
686// Return undefined or null to disable a query
687const { data, isEnabled } = useLiveQuery({
688 query: (q) => {
689 if (!userId) return undefined
690 return q.from({ todos: todosCollection })
691 .where(({ todos }) => eq(todos.userId, userId))
692 },
693})
694```
695​
696```ts
697// Join pattern
698const { data } = useLiveQuery({
699 query: (q) =>
700 q.from({ issues: issueCollection })
701 .join({ persons: personCollection }, ({ issues, persons }) =>
702 eq(issues.userId, persons.id)
703 )
704 .select(({ issues, persons }) => ({
705 id: issues.id,
706 title: issues.title,
707 userName: persons.name
708 }))
709})
710```
711​
712```ts
713// Handle loading and error states
714const { data, isLoading, isLoadingError, status } = useLiveQuery({
715 query: (q) => q.from({ todos: todoCollection })
716})
717​
718if (isLoading) return <div>Loading...</div>
719if (isLoadingError) return <div>Error: {status}</div>
720​
721return (
722 <ul>
723 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
724 </ul>
726```
727​
728## Call Signature
729​
730```ts
731function useLiveQuery<TContext>(config, deps?): object & LiveQueryStatusFlags & object;
732```
733​
734Defined in: [useLiveQuery.ts:537](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveQuery.ts#L537)
735​
736Create a live query using a query function.
737​
738### Type Parameters
739​
740#### TContext
741​
742`TContext` *extends* `Context`
743​
744### Parameters
745​
746#### config
747​
748`LiveQueryCollectionConfig`\<`TContext`\>
749​
750#### deps?
751​
752`unknown`[]
753​
754Deprecated array of dependencies that trigger query re-execution when changed
755​
756### Returns
757​
758Object with reactive data, state, and status information
759​
760### Examples
761​
762```ts
763// Prefer config object syntax
764const { data, isLoading } = useLiveQuery({
765 query: (q) =>
766 q.from({ todos: todosCollection })
767 .where(({ todos }) => eq(todos.completed, false))
768 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
769})
770```
771​
772```ts
773// Single result query
774const { data } = useLiveQuery({
775 query: (q) => q.from({ todos: todosCollection })
776 .where(({ todos }) => eq(todos.id, 1))
777 .findOne()
778})
779```
780​
781```ts
782// Structured captured values are included in derived query identity
783const { data, state } = useLiveQuery({
784 query: (q) => q.from({ todos: todosCollection })
785 .where(({ todos }) => gt(todos.priority, minPriority)),
786})
787```
788​
789```ts
790// Return undefined or null to disable a query
791const { data, isEnabled } = useLiveQuery({
792 query: (q) => {
793 if (!userId) return undefined
794 return q.from({ todos: todosCollection })
795 .where(({ todos }) => eq(todos.userId, userId))
796 },
797})
798```
799​
800```ts
801// Join pattern
802const { data } = useLiveQuery({
803 query: (q) =>
804 q.from({ issues: issueCollection })
805 .join({ persons: personCollection }, ({ issues, persons }) =>
806 eq(issues.userId, persons.id)
807 )
808 .select(({ issues, persons }) => ({
809 id: issues.id,
810 title: issues.title,
811 userName: persons.name
812 }))
813})
814```
815​
816```ts
817// Handle loading and error states
818const { data, isLoading, isLoadingError, status } = useLiveQuery({
819 query: (q) => q.from({ todos: todoCollection })
820})
821​
822if (isLoading) return <div>Loading...</div>
823if (isLoadingError) return <div>Error: {status}</div>
824​
825return (
826 <ul>
827 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
828 </ul>
830```
831​
832## Call Signature
833​
834```ts
835function useLiveQuery<TContext>(config, deps): object & LiveQueryStatusFlags & object;
836```
837​
838Defined in: [useLiveQuery.ts:549](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveQuery.ts#L549)
839​
840Create a live query using a query function.
841​
842### Type Parameters
843​
844#### TContext
845​
846`TContext` *extends* `Context`
847​
848### Parameters
849​
850#### config
851​
852[`ConditionalUseLiveQueryConfig`](../type-aliases/ConditionalUseLiveQueryConfig.md)\<`TContext`\>
853​
854#### deps
855​
856`unknown`[]
857​
858Deprecated array of dependencies that trigger query re-execution when changed
859​
860### Returns
861​
862Object with reactive data, state, and status information
863​
864### Examples
865​
866```ts
867// Prefer config object syntax
868const { data, isLoading } = useLiveQuery({
869 query: (q) =>
870 q.from({ todos: todosCollection })
871 .where(({ todos }) => eq(todos.completed, false))
872 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
873})
874```
875​
876```ts
877// Single result query
878const { data } = useLiveQuery({
879 query: (q) => q.from({ todos: todosCollection })
880 .where(({ todos }) => eq(todos.id, 1))
881 .findOne()
882})
883```
884​
885```ts
886// Structured captured values are included in derived query identity
887const { data, state } = useLiveQuery({
888 query: (q) => q.from({ todos: todosCollection })
889 .where(({ todos }) => gt(todos.priority, minPriority)),
890})
891```
892​
893```ts
894// Return undefined or null to disable a query
895const { data, isEnabled } = useLiveQuery({
896 query: (q) => {
897 if (!userId) return undefined
898 return q.from({ todos: todosCollection })
899 .where(({ todos }) => eq(todos.userId, userId))
900 },
901})
902```
903​
904```ts
905// Join pattern
906const { data } = useLiveQuery({
907 query: (q) =>
908 q.from({ issues: issueCollection })
909 .join({ persons: personCollection }, ({ issues, persons }) =>
910 eq(issues.userId, persons.id)
911 )
912 .select(({ issues, persons }) => ({
913 id: issues.id,
914 title: issues.title,
915 userName: persons.name
916 }))
917})
918```
919​
920```ts
921// Handle loading and error states
922const { data, isLoading, isLoadingError, status } = useLiveQuery({
923 query: (q) => q.from({ todos: todoCollection })
924})
925​
926if (isLoading) return <div>Loading...</div>
927if (isLoadingError) return <div>Error: {status}</div>
928​
929return (
930 <ul>
931 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
932 </ul>
934```
935​
936## Call Signature
937​
938```ts
939function useLiveQuery<TResult, TKey, TUtils>(liveQueryCollection): object & LiveQueryStatusFlags & object;
940```
941​
942Defined in: [useLiveQuery.ts:590](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveQuery.ts#L590)
943​
944Subscribe to an existing live query collection
945​
946### Type Parameters
947​
948#### TResult
949​
950`TResult` *extends* `object`
951​
952#### TKey
953​
954`TKey` *extends* `string` \| `number`
955​
956#### TUtils
957​
958`TUtils` *extends* `Record`\<`string`, `any`\>
959​
960### Parameters
961​
962#### liveQueryCollection
963​
964`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `NonSingleResult`
965​
966Pre-created live query collection to subscribe to
967​
968### Returns
969​
970Object with reactive data, state, and status information
971​
972### Examples
973​
974```ts
975// Using pre-created live query collection
976const myLiveQuery = createLiveQueryCollection((q) =>
977 q.from({ todos: todosCollection }).where(({ todos }) => eq(todos.active, true))
979const { data, collection } = useLiveQuery(myLiveQuery)
980```
981​
982```ts
983// Access collection methods directly
984const { data, collection, isReady } = useLiveQuery(existingCollection)
985​
986// Use collection for mutations
987const handleToggle = (id) => {
988 collection.update(id, draft => { draft.completed = !draft.completed })
990```
991​
992```ts
993// Handle states consistently
994const { data, isLoading, isLoadingError } = useLiveQuery(sharedCollection)
995​
996if (isLoading) return <div>Loading...</div>
997if (isLoadingError) return <div>Error loading data</div>
998​
999return <div>{data.map(item => <Item key={item.id} {...item} />)}</div>
1000```
1001​
1002## Call Signature
1003​
1004```ts
1005function useLiveQuery<TResult, TKey, TUtils>(liveQueryCollection): object & LiveQueryStatusFlags & object;
1006```
1007​
1008Defined in: [useLiveQuery.ts:605](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveQuery.ts#L605)
1009​
1010Create a live query using a query function.
1011​
1012### Type Parameters
1013​
1014#### TResult
1015​
1016`TResult` *extends* `object`
1017​
1018#### TKey
1019​
1020`TKey` *extends* `string` \| `number`
1021​
1022#### TUtils
1023​
1024`TUtils` *extends* `Record`\<`string`, `any`\>
1025​
1026### Parameters
1027​
1028#### liveQueryCollection
1029​
1030`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `SingleResult`
1031​
1032### Returns
1033​
1034Object with reactive data, state, and status information
1035​
1036### Examples
1037​
1038```ts
1039// Prefer config object syntax
1040const { data, isLoading } = useLiveQuery({
1041 query: (q) =>
1042 q.from({ todos: todosCollection })
1043 .where(({ todos }) => eq(todos.completed, false))
1044 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
1046```
1047​
1048```ts
1049// Single result query
1050const { data } = useLiveQuery({
1051 query: (q) => q.from({ todos: todosCollection })
1052 .where(({ todos }) => eq(todos.id, 1))
1053 .findOne()
1055```
1056​
1057```ts
1058// Structured captured values are included in derived query identity
1059const { data, state } = useLiveQuery({
1060 query: (q) => q.from({ todos: todosCollection })
1061 .where(({ todos }) => gt(todos.priority, minPriority)),
1063```
1064​
1065```ts
1066// Return undefined or null to disable a query
1067const { data, isEnabled } = useLiveQuery({
1068 query: (q) => {
1069 if (!userId) return undefined
1070 return q.from({ todos: todosCollection })
1071 .where(({ todos }) => eq(todos.userId, userId))
1072 },
1074```
1075​
1076```ts
1077// Join pattern
1078const { data } = useLiveQuery({
1079 query: (q) =>
1080 q.from({ issues: issueCollection })
1081 .join({ persons: personCollection }, ({ issues, persons }) =>
1082 eq(issues.userId, persons.id)
1084 .select(({ issues, persons }) => ({
1085 id: issues.id,
1086 title: issues.title,
1087 userName: persons.name
1088 }))
1090```
1091​
1092```ts
1093// Handle loading and error states
1094const { data, isLoading, isLoadingError, status } = useLiveQuery({
1095 query: (q) => q.from({ todos: todoCollection })
1097​
1098if (isLoading) return <div>Loading...</div>
1099if (isLoadingError) return <div>Error: {status}</div>
1100​
1101return (
1102 <ul>
1103 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
1104 </ul>
1106```