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