Open History to see the last commit
1---
2id: useLiveSuspenseQuery
3title: useLiveSuspenseQuery
4---
5
6## Call Signature
7
8```ts
9function useLiveSuspenseQuery<TContext>(queryFn, deps?): object;
10```
11
12Defined in: [useLiveSuspenseQuery.ts:156](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveSuspenseQuery.ts#L156)
13
14Create a live query with React Suspense support
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
38`object`
39
40Object with reactive data and state - data is guaranteed to be defined
41
42#### collection
43
44```ts
45collection: Collection<{ [K in string | number | symbol]: ResultValue<TContext>[K] }, string | number, {
46}>;
47```
48
49#### data
50
51```ts
52data: InferResultType<TContext>;
53```
54
55#### health
56
57```ts
58health: HealthNode | undefined;
59```
60
61#### state
62
63```ts
64state: Map<string | number, { [K in string | number | symbol]: ResultValue<TContext>[K] }>;
65```
66
67### Throws
68
69Promise when data is loading (caught by Suspense boundary)
70
71### Throws
72
73Error when the first load fails or the Collection crashes (caught by
74an Error boundary): the cause itself when it is an Error, otherwise an Error
75with the problem's message (`health.error.message`) whose `cause` is the
76value the sync adapter reported
77
78### Examples
79
80```ts
81// Basic usage with Suspense
82function TodoList() {
83 const { data } = useLiveSuspenseQuery({
84 query: (q) =>
85 q.from({ todos: todosCollection })
86 .where(({ todos }) => eq(todos.completed, false))
87 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
88 })
89
90 return (
91 <ul>
92 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
93 </ul>
94 )
95}
96
97function App() {
98 return (
99 <Suspense fallback={<div>Loading...</div>}>
100 <TodoList />
101 </Suspense>
102 )
103}
104```
105
106```ts
107// Single result query
108const { data } = useLiveSuspenseQuery(
109 (q) => q.from({ todos: todosCollection })
110 .where(({ todos }) => eq(todos.id, 1))
111 .findOne()
112)
113// data is guaranteed to be the single item (or undefined if not found)
114```
115
116```ts
117// Structured captured values are included in derived query identity and trigger re-suspension
118const { data } = useLiveSuspenseQuery({
119 query: (q) => q.from({ todos: todosCollection })
120 .where(({ todos }) => gt(todos.priority, minPriority)),
121})
122```
123
124```ts
125// With Error boundary
126function App() {
127 return (
128 <ErrorBoundary fallback={<div>Error loading data</div>}>
129 <Suspense fallback={<div>Loading...</div>}>
130 <TodoList />
131 </Suspense>
132 </ErrorBoundary>
133 )
134}
135```
136
137### Remarks
138
139**Important:** This hook does NOT support disabled queries (returning undefined/null).
140Following TanStack Query's useSuspenseQuery design, the query callback must always
141return a valid query, collection, or config object.
142
143❌ **This will cause a type error:**
144```ts
145useLiveSuspenseQuery(
146 (q) => userId ? q.from({ users }) : undefined // ❌ Error!
147)
148```
149
150✅ **Use conditional rendering instead:**
151```ts
152function Profile({ userId }: { userId: string }) {
153 const { data } = useLiveSuspenseQuery({
154 query: (q) => q.from({ users }).where(({ users }) => eq(users.id, userId)),
155 })
156 return <div>{data.name}</div>
157}
158
159// In parent component:
160{userId ? <Profile userId={userId} /> : <div>No user</div>}
161```
162
163✅ **For optional inputs, conditionally render a component with complete query inputs:**
164```ts
165{userId ? <Profile userId={userId} /> : <div>No user</div>}
166```
167
168## Call Signature
169
170```ts
171function useLiveSuspenseQuery<TContext>(config): object;
172```
173
174Defined in: [useLiveSuspenseQuery.ts:167](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveSuspenseQuery.ts#L167)
175
176Create a live query with React Suspense support
177
178### Type Parameters
179
180#### TContext
181
182`TContext` *extends* `Context`
183
184### Parameters
185
186#### config
187
188[`UseLiveQueryConfig`](../type-aliases/UseLiveQueryConfig.md)\<`TContext`\>
189
190### Returns
191
192`object`
193
194Object with reactive data and state - data is guaranteed to be defined
195
196#### collection
197
198```ts
199collection: Collection<{ [K in string | number | symbol]: ResultValue<TContext>[K] }, string | number, {
200}>;
201```
202
203#### data
204
205```ts
206data: InferResultType<TContext>;
207```
208
209#### health
210
211```ts
212health: HealthNode | undefined;
213```
214
215#### state
216
217```ts
218state: Map<string | number, { [K in string | number | symbol]: ResultValue<TContext>[K] }>;
219```
220
221### Throws
222
223Promise when data is loading (caught by Suspense boundary)
224
225### Throws
226
227Error when the first load fails or the Collection crashes (caught by
228an Error boundary): the cause itself when it is an Error, otherwise an Error
229with the problem's message (`health.error.message`) whose `cause` is the
230value the sync adapter reported
231
232### Examples
233
234```ts
235// Basic usage with Suspense
236function TodoList() {
237 const { data } = useLiveSuspenseQuery({
238 query: (q) =>
239 q.from({ todos: todosCollection })
240 .where(({ todos }) => eq(todos.completed, false))
241 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
242 })
243
244 return (
245 <ul>
246 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
247 </ul>
248 )
249}
250
251function App() {
252 return (
253 <Suspense fallback={<div>Loading...</div>}>
254 <TodoList />
255 </Suspense>
256 )
257}
258```
259
260```ts
261// Single result query
262const { data } = useLiveSuspenseQuery(
263 (q) => q.from({ todos: todosCollection })
264 .where(({ todos }) => eq(todos.id, 1))
265 .findOne()
266)
267// data is guaranteed to be the single item (or undefined if not found)
268```
269
270```ts
271// Structured captured values are included in derived query identity and trigger re-suspension
272const { data } = useLiveSuspenseQuery({
273 query: (q) => q.from({ todos: todosCollection })
274 .where(({ todos }) => gt(todos.priority, minPriority)),
275})
276```
277
278```ts
279// With Error boundary
280function App() {
281 return (
282 <ErrorBoundary fallback={<div>Error loading data</div>}>
283 <Suspense fallback={<div>Loading...</div>}>
284 <TodoList />
285 </Suspense>
286 </ErrorBoundary>
287 )
288}
289```
290
291### Remarks
292
293**Important:** This hook does NOT support disabled queries (returning undefined/null).
294Following TanStack Query's useSuspenseQuery design, the query callback must always
295return a valid query, collection, or config object.
296
297❌ **This will cause a type error:**
298```ts
299useLiveSuspenseQuery(
300 (q) => userId ? q.from({ users }) : undefined // ❌ Error!
301)
302```
303
304✅ **Use conditional rendering instead:**
305```ts
306function Profile({ userId }: { userId: string }) {
307 const { data } = useLiveSuspenseQuery({
308 query: (q) => q.from({ users }).where(({ users }) => eq(users.id, userId)),
309 })
310 return <div>{data.name}</div>
311}
312
313// In parent component:
314{userId ? <Profile userId={userId} /> : <div>No user</div>}
315```
316
317✅ **For optional inputs, conditionally render a component with complete query inputs:**
318```ts
319{userId ? <Profile userId={userId} /> : <div>No user</div>}
320```
321
322## Call Signature
323
324```ts
325function useLiveSuspenseQuery<TContext>(config, deps?): object;
326```
327
328Defined in: [useLiveSuspenseQuery.ts:177](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveSuspenseQuery.ts#L177)
329
330Create a live query with React Suspense support
331
332### Type Parameters
333
334#### TContext
335
336`TContext` *extends* `Context`
337
338### Parameters
339
340#### config
341
342`LiveQueryCollectionConfig`\<`TContext`\>
343
344#### deps?
345
346`unknown`[]
347
348Deprecated array of dependencies that trigger query re-execution when changed
349
350### Returns
351
352`object`
353
354Object with reactive data and state - data is guaranteed to be defined
355
356#### collection
357
358```ts
359collection: Collection<{ [K in string | number | symbol]: ResultValue<TContext>[K] }, string | number, {
360}>;
361```
362
363#### data
364
365```ts
366data: InferResultType<TContext>;
367```
368
369#### health
370
371```ts
372health: HealthNode | undefined;
373```
374
375#### state
376
377```ts
378state: Map<string | number, { [K in string | number | symbol]: ResultValue<TContext>[K] }>;
379```
380
381### Throws
382
383Promise when data is loading (caught by Suspense boundary)
384
385### Throws
386
387Error when the first load fails or the Collection crashes (caught by
388an Error boundary): the cause itself when it is an Error, otherwise an Error
389with the problem's message (`health.error.message`) whose `cause` is the
390value the sync adapter reported
391
392### Examples
393
394```ts
395// Basic usage with Suspense
396function TodoList() {
397 const { data } = useLiveSuspenseQuery({
398 query: (q) =>
399 q.from({ todos: todosCollection })
400 .where(({ todos }) => eq(todos.completed, false))
401 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
402 })
403
404 return (
405 <ul>
406 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
407 </ul>
408 )
409}
410
411function App() {
412 return (
413 <Suspense fallback={<div>Loading...</div>}>
414 <TodoList />
415 </Suspense>
416 )
417}
418```
419
420```ts
421// Single result query
422const { data } = useLiveSuspenseQuery(
423 (q) => q.from({ todos: todosCollection })
424 .where(({ todos }) => eq(todos.id, 1))
425 .findOne()
426)
427// data is guaranteed to be the single item (or undefined if not found)
428```
429
430```ts
431// Structured captured values are included in derived query identity and trigger re-suspension
432const { data } = useLiveSuspenseQuery({
433 query: (q) => q.from({ todos: todosCollection })
434 .where(({ todos }) => gt(todos.priority, minPriority)),
435})
436```
437
438```ts
439// With Error boundary
440function App() {
441 return (
442 <ErrorBoundary fallback={<div>Error loading data</div>}>
443 <Suspense fallback={<div>Loading...</div>}>
444 <TodoList />
445 </Suspense>
446 </ErrorBoundary>
447 )
448}
449```
450
451### Remarks
452
453**Important:** This hook does NOT support disabled queries (returning undefined/null).
454Following TanStack Query's useSuspenseQuery design, the query callback must always
455return a valid query, collection, or config object.
456
457❌ **This will cause a type error:**
458```ts
459useLiveSuspenseQuery(
460 (q) => userId ? q.from({ users }) : undefined // ❌ Error!
461)
462```
463
464✅ **Use conditional rendering instead:**
465```ts
466function Profile({ userId }: { userId: string }) {
467 const { data } = useLiveSuspenseQuery({
468 query: (q) => q.from({ users }).where(({ users }) => eq(users.id, userId)),
469 })
470 return <div>{data.name}</div>
471}
472
473// In parent component:
474{userId ? <Profile userId={userId} /> : <div>No user</div>}
475```
476
477✅ **For optional inputs, conditionally render a component with complete query inputs:**
478```ts
479{userId ? <Profile userId={userId} /> : <div>No user</div>}
480```
481
482## Call Signature
483
484```ts
485function useLiveSuspenseQuery<TResult, TKey, TUtils>(liveQueryCollection): object;
486```
487
488Defined in: [useLiveSuspenseQuery.ts:188](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveSuspenseQuery.ts#L188)
489
490Create a live query with React Suspense support
491
492### Type Parameters
493
494#### TResult
495
496`TResult` *extends* `object`
497
498#### TKey
499
500`TKey` *extends* `string` \| `number`
501
502#### TUtils
503
504`TUtils` *extends* `Record`\<`string`, `any`\>
505
506### Parameters
507
508#### liveQueryCollection
509
510`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `NonSingleResult`
511
512### Returns
513
514`object`
515
516Object with reactive data and state - data is guaranteed to be defined
517
518#### collection
519
520```ts
521collection: Collection<TResult, TKey, TUtils>;
522```
523
524#### data
525
526```ts
527data: TResult[];
528```
529
530#### health
531
532```ts
533health: HealthNode | undefined;
534```
535
536#### state
537
538```ts
539state: Map<TKey, TResult>;
540```
541
542### Throws
543
544Promise when data is loading (caught by Suspense boundary)
545
546### Throws
547
548Error when the first load fails or the Collection crashes (caught by
549an Error boundary): the cause itself when it is an Error, otherwise an Error
550with the problem's message (`health.error.message`) whose `cause` is the
551value the sync adapter reported
552
553### Examples
554
555```ts
556// Basic usage with Suspense
557function TodoList() {
558 const { data } = useLiveSuspenseQuery({
559 query: (q) =>
560 q.from({ todos: todosCollection })
561 .where(({ todos }) => eq(todos.completed, false))
562 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
563 })
564
565 return (
566 <ul>
567 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
568 </ul>
569 )
570}
571
572function App() {
573 return (
574 <Suspense fallback={<div>Loading...</div>}>
575 <TodoList />
576 </Suspense>
577 )
578}
579```
580
581```ts
582// Single result query
583const { data } = useLiveSuspenseQuery(
584 (q) => q.from({ todos: todosCollection })
585 .where(({ todos }) => eq(todos.id, 1))
586 .findOne()
587)
588// data is guaranteed to be the single item (or undefined if not found)
589```
590
591```ts
592// Structured captured values are included in derived query identity and trigger re-suspension
593const { data } = useLiveSuspenseQuery({
594 query: (q) => q.from({ todos: todosCollection })
595 .where(({ todos }) => gt(todos.priority, minPriority)),
596})
597```
598
599```ts
600// With Error boundary
601function App() {
602 return (
603 <ErrorBoundary fallback={<div>Error loading data</div>}>
604 <Suspense fallback={<div>Loading...</div>}>
605 <TodoList />
606 </Suspense>
607 </ErrorBoundary>
608 )
609}
610```
611
612### Remarks
613
614**Important:** This hook does NOT support disabled queries (returning undefined/null).
615Following TanStack Query's useSuspenseQuery design, the query callback must always
616return a valid query, collection, or config object.
617
618❌ **This will cause a type error:**
619```ts
620useLiveSuspenseQuery(
621 (q) => userId ? q.from({ users }) : undefined // ❌ Error!
622)
623```
624
625✅ **Use conditional rendering instead:**
626```ts
627function Profile({ userId }: { userId: string }) {
628 const { data } = useLiveSuspenseQuery({
629 query: (q) => q.from({ users }).where(({ users }) => eq(users.id, userId)),
630 })
631 return <div>{data.name}</div>
632}
633
634// In parent component:
635{userId ? <Profile userId={userId} /> : <div>No user</div>}
636```
637
638✅ **For optional inputs, conditionally render a component with complete query inputs:**
639```ts
640{userId ? <Profile userId={userId} /> : <div>No user</div>}
641```
642
643## Call Signature
644
645```ts
646function useLiveSuspenseQuery<TResult, TKey, TUtils>(liveQueryCollection): object;
647```
648
649Defined in: [useLiveSuspenseQuery.ts:202](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveSuspenseQuery.ts#L202)
650
651Create a live query with React Suspense support
652
653### Type Parameters
654
655#### TResult
656
657`TResult` *extends* `object`
658
659#### TKey
660
661`TKey` *extends* `string` \| `number`
662
663#### TUtils
664
665`TUtils` *extends* `Record`\<`string`, `any`\>
666
667### Parameters
668
669#### liveQueryCollection
670
671`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `SingleResult`
672
673### Returns
674
675`object`
676
677Object with reactive data and state - data is guaranteed to be defined
678
679#### collection
680
681```ts
682collection: Collection<TResult, TKey, TUtils, StandardSchemaV1<unknown, unknown>, TResult> & SingleResult;
683```
684
685#### data
686
687```ts
688data: TResult | undefined;
689```
690
691#### health
692
693```ts
694health: HealthNode | undefined;
695```
696
697#### state
698
699```ts
700state: Map<TKey, TResult>;
701```
702
703### Throws
704
705Promise when data is loading (caught by Suspense boundary)
706
707### Throws
708
709Error when the first load fails or the Collection crashes (caught by
710an Error boundary): the cause itself when it is an Error, otherwise an Error
711with the problem's message (`health.error.message`) whose `cause` is the
712value the sync adapter reported
713
714### Examples
715
716```ts
717// Basic usage with Suspense
718function TodoList() {
719 const { data } = useLiveSuspenseQuery({
720 query: (q) =>
721 q.from({ todos: todosCollection })
722 .where(({ todos }) => eq(todos.completed, false))
723 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
724 })
725
726 return (
727 <ul>
728 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
729 </ul>
730 )
731}
732
733function App() {
734 return (
735 <Suspense fallback={<div>Loading...</div>}>
736 <TodoList />
737 </Suspense>
738 )
739}
740```
741
742```ts
743// Single result query
744const { data } = useLiveSuspenseQuery(
745 (q) => q.from({ todos: todosCollection })
746 .where(({ todos }) => eq(todos.id, 1))
747 .findOne()
748)
749// data is guaranteed to be the single item (or undefined if not found)
750```
751
752```ts
753// Structured captured values are included in derived query identity and trigger re-suspension
754const { data } = useLiveSuspenseQuery({
755 query: (q) => q.from({ todos: todosCollection })
756 .where(({ todos }) => gt(todos.priority, minPriority)),
757})
758```
759
760```ts
761// With Error boundary
762function App() {
763 return (
764 <ErrorBoundary fallback={<div>Error loading data</div>}>
765 <Suspense fallback={<div>Loading...</div>}>
766 <TodoList />
767 </Suspense>
768 </ErrorBoundary>
769 )
770}
771```
772
773### Remarks
774
775**Important:** This hook does NOT support disabled queries (returning undefined/null).
776Following TanStack Query's useSuspenseQuery design, the query callback must always
777return a valid query, collection, or config object.
778
779❌ **This will cause a type error:**
780```ts
781useLiveSuspenseQuery(
782 (q) => userId ? q.from({ users }) : undefined // ❌ Error!
783)
784```
785
786✅ **Use conditional rendering instead:**
787```ts
788function Profile({ userId }: { userId: string }) {
789 const { data } = useLiveSuspenseQuery({
790 query: (q) => q.from({ users }).where(({ users }) => eq(users.id, userId)),
791 })
792 return <div>{data.name}</div>
793}
794
795// In parent component:
796{userId ? <Profile userId={userId} /> : <div>No user</div>}
797```
798
799✅ **For optional inputs, conditionally render a component with complete query inputs:**
800```ts
801{userId ? <Profile userId={userId} /> : <div>No user</div>}
802```