Using useInfiniteQuery() with localStorage persister isn't working as expected #9246
Unanswered
GarrisonBates
asked this question in
Q&A
Replies: 1 comment 4 replies
-
I've gone through all the docs related to infinite query and I'm pretty sure this is what's going on:
So the question is really, is there any sort of built-in way to avoid this behavior? I'm pretty sure the empty array is necessary for RQ to know not to keep triggering a request every time the intersection observer comes into view, but I don't want it breaking the functionality like it's currently doing. Any ideas? Much appreciated. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I'm using infinite queries that fetch both ways. For context, it's a messaging feature within my app with a maxPages limit, so I need to be able to paginate both forward and backward. I've set up infinite queries for several other data types so I didn't expect this to be too tricky. But the bidirectional aspect has added a lot more complication than I would've hoped.
The actual infinite scroll works fine. If I delete the localStorage cache, on the first page load it fetches fine. Then when I scroll upwards, it fetches the older messages correctly.
But if I refresh the page, suddenly no messages are fetched. Seems like it may have something to do with the fact that since my "get previous page" intersection observer is triggered on first mount, RQ attempts to fetch previous page, which returns an empty array. So I end up with
pages
that looks like:[ [], [<actual content>] ]
When the page is refreshed, only one fetch request is sent, and it uses the pageParam from the very latest message, trying to fetch messages newer than the newest message. In other words, it returns empty array, and no messages are fetched.
Does anyone know why this may be happening? Is there a way to use the persister with infinite query cleanly?
This is my hook:
Might be worth noting that I tried it the other way around first (i.e. newer messages were the last page, older messages were the first page). Thought maybe the first page was always supposed to be the most recent so I switched to current version, but the behavior didn't change.
My basic set up, I have an intersection observer at both the top and bottom of the scroll container. When intersection observer is in view, it triggers fetchNextPage or fetchPreviousPage if conditions are right:
Beta Was this translation helpful? Give feedback.
All reactions