-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
Describe the bug
Infinite Queries can be used in two ways:
- they implement
getNextPageParam / getPreviousPageParam(automatic) - they pass
pageParamdirectly tofetchNextPage / fetchPreviousPage(manual)
Mixing those two leads to a weird behaviour.
Your minimal, reproducible example
https://codesandbox.io/s/dazzling-lehmann-zibq9m?file=/pages/index.js
Steps to reproduce
- scroll down, click on
load newer - now, click the
skipbutton - you now have projects 0-9 and 50-54 on screen. The stored
pageParamsare:
[0, 5, 50] - trigger an automatic refetch by re-focussing the window
--> you now have projects 0-14 on screen
pageParmas are:
[0, 5, 10]
Expected behavior
data on screen should not change / stay as it was initially fetched, and pageParams shouldn't change.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
all
react-query version
4
TypeScript version
No response
Additional context
The problem seems to be that during an automatic refetch, getNextParam is always invoked when it exists. We have no knowledge of the fact that the 3rd page was fetched with the "manual" approach.
It seems that mixing both approaches is not supported at all, even though there is an extra section in the docs that does exactly that:
when we refetch pages, we determine if we are in manual mode or not by the presence of the getNextPageParam function:
| const manual = typeof context.options.getNextPageParam === 'undefined' |
But that function will always be present, even when a single page fetch was "overwritten" with a manual fetch.
One way to fix this would be to extend pageParams (or the whole infinite query data structure) by the information if the fetch happened initially as manual or not.
Actually, we only store pageParams for refetches when everything is using the manual approach. Put another way: We never access pageParams if a getNextPageParam function is present.