Skip to content

Commit 42a9dc6

Browse files
committed
changeset
1 parent 2e7f238 commit 42a9dc6

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

.changeset/smooth-goats-ring.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
"@tanstack/react-db": patch
3+
---
4+
5+
Add `useLiveInfiniteQuery` hook for infinite scrolling with live updates.
6+
7+
The new `useLiveInfiniteQuery` hook provides an infinite query pattern similar to TanStack Query's `useInfiniteQuery`, but with live updates from your local collection. It uses `liveQueryCollection.utils.setWindow()` internally to efficiently paginate through ordered data without recreating the query on each page fetch.
8+
9+
**Key features:**
10+
- Automatic live updates as data changes in the collection
11+
- Efficient pagination using dynamic window adjustment
12+
- Peek-ahead mechanism to detect when more pages are available
13+
- Compatible with TanStack Query's infinite query API patterns
14+
15+
**Example usage:**
16+
17+
```tsx
18+
import { useLiveInfiniteQuery } from '@tanstack/react-db'
19+
20+
function PostList() {
21+
const { data, pages, fetchNextPage, hasNextPage, isLoading } = useLiveInfiniteQuery(
22+
(q) => q
23+
.from({ posts: postsCollection })
24+
.orderBy(({ posts }) => posts.createdAt, 'desc'),
25+
{
26+
pageSize: 20,
27+
getNextPageParam: (lastPage, allPages) =>
28+
lastPage.length === 20 ? allPages.length : undefined
29+
}
30+
)
31+
32+
if (isLoading) return <div>Loading...</div>
33+
34+
return (
35+
<div>
36+
{pages.map((page, i) => (
37+
<div key={i}>
38+
{page.map(post => (
39+
<PostCard key={post.id} post={post} />
40+
))}
41+
</div>
42+
))}
43+
{hasNextPage && (
44+
<button onClick={() => fetchNextPage()}>
45+
Load More
46+
</button>
47+
)}
48+
</div>
49+
)
50+
}
51+
```
52+
53+
**Requirements:**
54+
- Query must include `.orderBy()` for the window mechanism to work
55+
- Returns flattened `data` array and `pages` array for flexible rendering
56+
- Automatically detects new pages when data is synced to the collection
57+

0 commit comments

Comments
 (0)