Skip to content

Commit 24fccb5

Browse files
committed
expand useLiveQuery callback to support conditional queries and additional return types
1 parent b5d4210 commit 24fccb5

File tree

3 files changed

+628
-55
lines changed

3 files changed

+628
-55
lines changed

.changeset/spotty-dogs-fry.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
"@tanstack/react-db": patch
3+
---
4+
5+
Expand `useLiveQuery` callback to support conditional queries and additional return types, enabling the ability to temporarily disable the query.
6+
7+
**New Features:**
8+
9+
- Callback can now return `undefined` or `null` to temporarily disable the query
10+
- Callback can return a pre-created `Collection` instance to use it directly
11+
- Callback can return a `LiveQueryCollectionConfig` object for advanced configuration
12+
- When disabled (returning `undefined`/`null`), the hook returns a specific idle state
13+
14+
**Usage Examples:**
15+
16+
```ts
17+
// Conditional queries - disable when not ready
18+
const enabled = useState(false)
19+
const { data, state, isIdle } = useLiveQuery((q) => {
20+
if (!enabled) return undefined // Disables the query
21+
return q.from({ users }).where(...)
22+
}, [enabled])
23+
24+
/**
25+
* When disabled, returns:
26+
* {
27+
* state: undefined,
28+
* data: undefined,
29+
* isIdle: true,
30+
* ...
31+
* }
32+
*/
33+
34+
// Return pre-created Collection
35+
const { data } = useLiveQuery((q) => {
36+
if (usePrebuilt) return myCollection // Use existing collection
37+
return q.from({ items }).select(...)
38+
}, [usePrebuilt])
39+
40+
// Return LiveQueryCollectionConfig
41+
const { data } = useLiveQuery((q) => {
42+
return {
43+
query: q.from({ items }).select(...),
44+
id: `my-collection`,
45+
}
46+
})
47+
```

0 commit comments

Comments
 (0)