Open History to see the last commit
1---
2id: useLiveSuspenseQuery
3title: useLiveSuspenseQuery
4---
5
6# Function: useLiveSuspenseQuery()
7
8## Call Signature
9
10```ts
11function useLiveSuspenseQuery<TContext>(queryFn, deps?): object;
12```
13
14Defined in: [useLiveSuspenseQuery.ts:109](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveSuspenseQuery.ts#L109)
15
16Create a live query with React Suspense support
17
18### Type Parameters
19
20#### TContext
21
22`TContext` *extends* `Context`
23
24### Parameters
25
26#### queryFn
27
28(`q`) => `QueryBuilder`\<`TContext`\>
29
30Query function that defines what data to fetch
31
32#### deps?
33
34`unknown`[]
35
36Array of dependencies that trigger query re-execution when changed
37
38### Returns
39
40`object`
41
42Object with reactive data and state - data is guaranteed to be defined
43
44#### collection
45
46```ts
47collection: Collection<{ [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }, string | number, {
48}>;
49```
50
51#### data
52
53```ts
54data: InferResultType<TContext>;
55```
56
57#### state
58
59```ts
60state: Map<string | number, { [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }>;
61```
62
63### Throws
64
65Promise when data is loading (caught by Suspense boundary)
66
67### Throws
68
69Error when collection fails (caught by Error boundary)
70
71### Examples
72
73```ts
74// Basic usage with Suspense
75function TodoList() {
76 const { data } = useLiveSuspenseQuery((q) =>
77 q.from({ todos: todosCollection })
78 .where(({ todos }) => eq(todos.completed, false))
79 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
80 )
81
82 return (
83 <ul>
84 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
85 </ul>
86 )
87}
88
89function App() {
90 return (
91 <Suspense fallback={<div>Loading...</div>}>
92 <TodoList />
93 </Suspense>
94 )
95}
96```
97
98```ts
99// Single result query
100const { data } = useLiveSuspenseQuery(
101 (q) => q.from({ todos: todosCollection })
102 .where(({ todos }) => eq(todos.id, 1))
103 .findOne()
104)
105// data is guaranteed to be the single item (or undefined if not found)
106```
107
108```ts
109// With dependencies that trigger re-suspension
110const { data } = useLiveSuspenseQuery(
111 (q) => q.from({ todos: todosCollection })
112 .where(({ todos }) => gt(todos.priority, minPriority)),
113 [minPriority] // Re-suspends when minPriority changes
114)
115```
116
117```ts
118// With Error boundary
119function App() {
120 return (
121 <ErrorBoundary fallback={<div>Error loading data</div>}>
122 <Suspense fallback={<div>Loading...</div>}>
123 <TodoList />
124 </Suspense>
125 </ErrorBoundary>
126 )
127}
128```
129
130### Remarks
131
132**Important:** This hook does NOT support disabled queries (returning undefined/null).
133Following TanStack Query's useSuspenseQuery design, the query callback must always
134return a valid query, collection, or config object.
135
136❌ **This will cause a type error:**
137```ts
138useLiveSuspenseQuery(
139 (q) => userId ? q.from({ users }) : undefined // ❌ Error!
140)
141```
142
143✅ **Use conditional rendering instead:**
144```ts
145function Profile({ userId }: { userId: string }) {
146 const { data } = useLiveSuspenseQuery(
147 (q) => q.from({ users }).where(({ users }) => eq(users.id, userId))
148 )
149 return <div>{data.name}</div>
150}
151
152// In parent component:
153{userId ? <Profile userId={userId} /> : <div>No user</div>}
154```
155
156✅ **Or use useLiveQuery for conditional queries:**
157```ts
158const { data, isEnabled } = useLiveQuery(
159 (q) => userId ? q.from({ users }) : undefined, // ✅ Supported!
160 [userId]
161)
162```
163
164## Call Signature
165
166```ts
167function useLiveSuspenseQuery<TContext>(config, deps?): object;
168```
169
170Defined in: [useLiveSuspenseQuery.ts:119](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveSuspenseQuery.ts#L119)
171
172Create a live query with React Suspense support
173
174### Type Parameters
175
176#### TContext
177
178`TContext` *extends* `Context`
179
180### Parameters
181
182#### config
183
184`LiveQueryCollectionConfig`\<`TContext`\>
185
186#### deps?
187
188`unknown`[]
189
190Array of dependencies that trigger query re-execution when changed
191
192### Returns
193
194`object`
195
196Object with reactive data and state - data is guaranteed to be defined
197
198#### collection
199
200```ts
201collection: Collection<{ [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }, string | number, {
202}>;
203```
204
205#### data
206
207```ts
208data: InferResultType<TContext>;
209```
210
211#### state
212
213```ts
214state: Map<string | number, { [K in string | number | symbol]: (TContext["result"] extends object ? any[any] : TContext["hasJoins"] extends true ? TContext["schema"] : TContext["schema"][TContext["fromSourceName"]])[K] }>;
215```
216
217### Throws
218
219Promise when data is loading (caught by Suspense boundary)
220
221### Throws
222
223Error when collection fails (caught by Error boundary)
224
225### Examples
226
227```ts
228// Basic usage with Suspense
229function TodoList() {
230 const { data } = useLiveSuspenseQuery((q) =>
231 q.from({ todos: todosCollection })
232 .where(({ todos }) => eq(todos.completed, false))
233 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
234 )
235
236 return (
237 <ul>
238 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
239 </ul>
240 )
241}
242
243function App() {
244 return (
245 <Suspense fallback={<div>Loading...</div>}>
246 <TodoList />
247 </Suspense>
248 )
249}
250```
251
252```ts
253// Single result query
254const { data } = useLiveSuspenseQuery(
255 (q) => q.from({ todos: todosCollection })
256 .where(({ todos }) => eq(todos.id, 1))
257 .findOne()
258)
259// data is guaranteed to be the single item (or undefined if not found)
260```
261
262```ts
263// With dependencies that trigger re-suspension
264const { data } = useLiveSuspenseQuery(
265 (q) => q.from({ todos: todosCollection })
266 .where(({ todos }) => gt(todos.priority, minPriority)),
267 [minPriority] // Re-suspends when minPriority changes
268)
269```
270
271```ts
272// With Error boundary
273function App() {
274 return (
275 <ErrorBoundary fallback={<div>Error loading data</div>}>
276 <Suspense fallback={<div>Loading...</div>}>
277 <TodoList />
278 </Suspense>
279 </ErrorBoundary>
280 )
281}
282```
283
284### Remarks
285
286**Important:** This hook does NOT support disabled queries (returning undefined/null).
287Following TanStack Query's useSuspenseQuery design, the query callback must always
288return a valid query, collection, or config object.
289
290❌ **This will cause a type error:**
291```ts
292useLiveSuspenseQuery(
293 (q) => userId ? q.from({ users }) : undefined // ❌ Error!
294)
295```
296
297✅ **Use conditional rendering instead:**
298```ts
299function Profile({ userId }: { userId: string }) {
300 const { data } = useLiveSuspenseQuery(
301 (q) => q.from({ users }).where(({ users }) => eq(users.id, userId))
302 )
303 return <div>{data.name}</div>
304}
305
306// In parent component:
307{userId ? <Profile userId={userId} /> : <div>No user</div>}
308```
309
310✅ **Or use useLiveQuery for conditional queries:**
311```ts
312const { data, isEnabled } = useLiveQuery(
313 (q) => userId ? q.from({ users }) : undefined, // ✅ Supported!
314 [userId]
315)
316```
317
318## Call Signature
319
320```ts
321function useLiveSuspenseQuery<TResult, TKey, TUtils>(liveQueryCollection): object;
322```
323
324Defined in: [useLiveSuspenseQuery.ts:129](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveSuspenseQuery.ts#L129)
325
326Create a live query with React Suspense support
327
328### Type Parameters
329
330#### TResult
331
332`TResult` *extends* `object`
333
334#### TKey
335
336`TKey` *extends* `string` \| `number`
337
338#### TUtils
339
340`TUtils` *extends* `Record`\<`string`, `any`\>
341
342### Parameters
343
344#### liveQueryCollection
345
346`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `NonSingleResult`
347
348### Returns
349
350`object`
351
352Object with reactive data and state - data is guaranteed to be defined
353
354#### collection
355
356```ts
357collection: Collection<TResult, TKey, TUtils>;
358```
359
360#### data
361
362```ts
363data: TResult[];
364```
365
366#### state
367
368```ts
369state: Map<TKey, TResult>;
370```
371
372### Throws
373
374Promise when data is loading (caught by Suspense boundary)
375
376### Throws
377
378Error when collection fails (caught by Error boundary)
379
380### Examples
381
382```ts
383// Basic usage with Suspense
384function TodoList() {
385 const { data } = useLiveSuspenseQuery((q) =>
386 q.from({ todos: todosCollection })
387 .where(({ todos }) => eq(todos.completed, false))
388 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
389 )
390
391 return (
392 <ul>
393 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
394 </ul>
395 )
396}
397
398function App() {
399 return (
400 <Suspense fallback={<div>Loading...</div>}>
401 <TodoList />
402 </Suspense>
403 )
404}
405```
406
407```ts
408// Single result query
409const { data } = useLiveSuspenseQuery(
410 (q) => q.from({ todos: todosCollection })
411 .where(({ todos }) => eq(todos.id, 1))
412 .findOne()
413)
414// data is guaranteed to be the single item (or undefined if not found)
415```
416
417```ts
418// With dependencies that trigger re-suspension
419const { data } = useLiveSuspenseQuery(
420 (q) => q.from({ todos: todosCollection })
421 .where(({ todos }) => gt(todos.priority, minPriority)),
422 [minPriority] // Re-suspends when minPriority changes
423)
424```
425
426```ts
427// With Error boundary
428function App() {
429 return (
430 <ErrorBoundary fallback={<div>Error loading data</div>}>
431 <Suspense fallback={<div>Loading...</div>}>
432 <TodoList />
433 </Suspense>
434 </ErrorBoundary>
435 )
436}
437```
438
439### Remarks
440
441**Important:** This hook does NOT support disabled queries (returning undefined/null).
442Following TanStack Query's useSuspenseQuery design, the query callback must always
443return a valid query, collection, or config object.
444
445❌ **This will cause a type error:**
446```ts
447useLiveSuspenseQuery(
448 (q) => userId ? q.from({ users }) : undefined // ❌ Error!
449)
450```
451
452✅ **Use conditional rendering instead:**
453```ts
454function Profile({ userId }: { userId: string }) {
455 const { data } = useLiveSuspenseQuery(
456 (q) => q.from({ users }).where(({ users }) => eq(users.id, userId))
457 )
458 return <div>{data.name}</div>
459}
460
461// In parent component:
462{userId ? <Profile userId={userId} /> : <div>No user</div>}
463```
464
465✅ **Or use useLiveQuery for conditional queries:**
466```ts
467const { data, isEnabled } = useLiveQuery(
468 (q) => userId ? q.from({ users }) : undefined, // ✅ Supported!
469 [userId]
470)
471```
472
473## Call Signature
474
475```ts
476function useLiveSuspenseQuery<TResult, TKey, TUtils>(liveQueryCollection): object;
477```
478
479Defined in: [useLiveSuspenseQuery.ts:142](https://github.com/TanStack/db/blob/main/packages/react-db/src/useLiveSuspenseQuery.ts#L142)
480
481Create a live query with React Suspense support
482
483### Type Parameters
484
485#### TResult
486
487`TResult` *extends* `object`
488
489#### TKey
490
491`TKey` *extends* `string` \| `number`
492
493#### TUtils
494
495`TUtils` *extends* `Record`\<`string`, `any`\>
496
497### Parameters
498
499#### liveQueryCollection
500
501`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `SingleResult`
502
503### Returns
504
505`object`
506
507Object with reactive data and state - data is guaranteed to be defined
508
509#### collection
510
511```ts
512collection: Collection<TResult, TKey, TUtils, StandardSchemaV1<unknown, unknown>, TResult> & SingleResult;
513```
514
515#### data
516
517```ts
518data: TResult | undefined;
519```
520
521#### state
522
523```ts
524state: Map<TKey, TResult>;
525```
526
527### Throws
528
529Promise when data is loading (caught by Suspense boundary)
530
531### Throws
532
533Error when collection fails (caught by Error boundary)
534
535### Examples
536
537```ts
538// Basic usage with Suspense
539function TodoList() {
540 const { data } = useLiveSuspenseQuery((q) =>
541 q.from({ todos: todosCollection })
542 .where(({ todos }) => eq(todos.completed, false))
543 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
544 )
545
546 return (
547 <ul>
548 {data.map(todo => <li key={todo.id}>{todo.text}</li>)}
549 </ul>
550 )
551}
552
553function App() {
554 return (
555 <Suspense fallback={<div>Loading...</div>}>
556 <TodoList />
557 </Suspense>
558 )
559}
560```
561
562```ts
563// Single result query
564const { data } = useLiveSuspenseQuery(
565 (q) => q.from({ todos: todosCollection })
566 .where(({ todos }) => eq(todos.id, 1))
567 .findOne()
568)
569// data is guaranteed to be the single item (or undefined if not found)
570```
571
572```ts
573// With dependencies that trigger re-suspension
574const { data } = useLiveSuspenseQuery(
575 (q) => q.from({ todos: todosCollection })
576 .where(({ todos }) => gt(todos.priority, minPriority)),
577 [minPriority] // Re-suspends when minPriority changes
578)
579```
580
581```ts
582// With Error boundary
583function App() {
584 return (
585 <ErrorBoundary fallback={<div>Error loading data</div>}>
586 <Suspense fallback={<div>Loading...</div>}>
587 <TodoList />
588 </Suspense>
589 </ErrorBoundary>
590 )
591}
592```
593
594### Remarks
595
596**Important:** This hook does NOT support disabled queries (returning undefined/null).
597Following TanStack Query's useSuspenseQuery design, the query callback must always
598return a valid query, collection, or config object.
599
600❌ **This will cause a type error:**
601```ts
602useLiveSuspenseQuery(
603 (q) => userId ? q.from({ users }) : undefined // ❌ Error!
604)
605```
606
607✅ **Use conditional rendering instead:**
608```ts
609function Profile({ userId }: { userId: string }) {
610 const { data } = useLiveSuspenseQuery(
611 (q) => q.from({ users }).where(({ users }) => eq(users.id, userId))
612 )
613 return <div>{data.name}</div>
614}
615
616// In parent component:
617{userId ? <Profile userId={userId} /> : <div>No user</div>}
618```
619
620✅ **Or use useLiveQuery for conditional queries:**
621```ts
622const { data, isEnabled } = useLiveQuery(
623 (q) => userId ? q.from({ users }) : undefined, // ✅ Supported!
624 [userId]
625)
626```