Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

InfiniteQueryObserver.md

26 lines667 Bytes

1---
2id: InfiniteQueryObserver
3title: InfiniteQueryObserver
4---
5​
6## `InfiniteQueryObserver`
7​
8The `InfiniteQueryObserver` can be used to observe and switch between infinite queries.
9​
10```js
11const observer = new InfiniteQueryObserver(queryClient, {
12 queryKey: 'posts',
13 queryFn: fetchPosts,
14 getNextPageParam: (lastPage, allPages) => lastPage.nextCursor,
15 getPreviousPageParam: (firstPage, allPages) => firstPage.prevCursor,
16})
17​
18const unsubscribe = observer.subscribe(result => {
19 console.log(result)
20 unsubscribe()
21})
22```
23​
24**Options**
25​
26The options for the `InfiniteQueryObserver` are exactly the same as those of [`useInfiniteQuery`](/reference/useInfiniteQuery).