Skip to content

Commit 8648213

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

File tree

3 files changed

+627
-55
lines changed

3 files changed

+627
-55
lines changed

.changeset/spotty-dogs-fry.md

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

0 commit comments

Comments
 (0)