useInfiniteQuery not working properly after removing version 3.0.1
See original GitHub issueDescribe the bug
After I was forced to downgrade from v3.0.1
to v2.23.0
, because the v3.0.1
was completely deleted from its existence, the useInfiniteQuery
is not working properly anymore.
Few issues happening
- It’s fetching the API twice for each page when I scroll to bottom
- Sometimes it doesn’t even call the API to fetch data when I scroll to bottom
- The refetch function is sending a
null
page if the previous page has been already fetched, for example, if I scroll to bottom and fetch the next page (it’ll fetch it twice for some reason) then when I call refetch it’s sending once with the correct page number and another time with valuenull
(I assume because of some caching issue cause previously it called the. API twice with that page number) .
To Reproduce Steps to reproduce the behavior:
- I don’t know how to reproduce this outside of my app but I am pretty sure if you guys test this and use for your own app you will see the exact same problems
Expected behavior
- When I scroll to bottom I expect to fetch the next page data only once
- Detect scroll to bottom always and fetch only if there is more data to fetch
- When calling
refetch
it should fetch all previous data called (with all pages)
Screenshots If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
- OS: MacOS Catalina
- Browser: Chrome
Additional context Please bring back version v3.0.1 because my app now is not working or else check what have you guys missed from v3.0.1 in v2.23.0.
Part of my code I can see the initial data loaded but when scrolling to bottom then all the issues are coming up.
const Component = () => {
const {
data,
isFetchingMore,
isFetching,
fetchMore,
canFetchMore,
refetch,
} = useInfiniteQuery(
'fetch-data',
async (key, page: number | null = 1) =>
getData(id, page, filter),
{
getFetchMore: (latestFetch) => latestFetch.nextPage
}
)
useIntersectionObserver({
target: loadMoreRef,
onIntersect: fetchMore,
enabled: canFetchMore,
})
useEffect(() => {
refetch()
}, [filter])
return (
<div className="list">
{data && data.map((page) =>
page.data.length > 0 ? (
page.data.map((user: Account) => (
<Item> {user.name} </Item>
))
) : (
<div> no user found </div>
)
)}
{canFetchMore && (
<div ref={loadMoreRef}>
{isFetchingMore && <LoadingSpinner />}
</div>
)}
}
</div>
)
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top Results From Across the Web
useInfiniteQuery not working properly after removing version ...
0.1 was completely deleted from its existence, the useInfiniteQuery is not working properly anymore. Few issues happening. It's fetching the API ...
Read more >useInfiniteQuery not being able to update nextPageParams
while the first page of the infinite query returns result just fine (pageParam=0), it has not been able to update the nextPageParam as...
Read more >Infinite Queries | TanStack Query Docs
React Query supports a useful version of useQuery called useInfiniteQuery for querying these types of lists. When using useInfiniteQuery , you'll notice a ......
Read more >Infinite Scroll in React | Full Tutorial - YouTube
Infinite Scroll in React is a highly requested app feature that can be achieved in several ways. This React full tutorial provides two ......
Read more >React Query v1.0.0 released - Morioh
React Query exports a set of hooks that address these issues. ... React Query supports a useful version of useQuery called useInfiniteQuery for...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
useRef
will not notify you when it’s content changes, simply change tocallback ref
may solve your problemThanks @kennedy0527 this solves my loading twice issue