Bi-directional infinite query will not fetch previous page if initial prevCursor is null #8330
Send to my agentDescribe the bug#
Bi-directional infinite query will not fetch previous page if initial prevCursor is null. I believe this is a bug caused by using bi-directional useInfiniteQuery with maxPages. Please see the codesandbox link provided.
Your minimal, reproducible example#
https://codesandbox.io/p/github/Apestein/tanstack-query-bug-demo/main?import=true
Steps to reproduce#
Observe the behavior of the infinite scroll container, which is a bi-directional infinite scroll component. It will work as intended (scroll to 49, then scroll back). Now uncomment line 18 in the actions.ts file. Now inverse scrolling will no longer work (scroll to 49 then try to scroll back). The intended behavior is to be able to infinite scroll forward and back. Maybe I'm not seeing something but I think this is a bug.
Expected behavior#
The intended behavior is for pageParam 0 to be the last page, meaning 0 is the last number that will appear. Maybe I'm not seeing something but I think this is a bug.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
OS: ubuntu linux Browser: chrome
Tanstack Query adapter#
None
TanStack Query version#
v5.60.2
TypeScript version#
v5
Additional context#
No response
the sandbox is inaccessible - likely because it’s private or in your drafts. Please make it public
my bad, it's fixed
still doesn’t work for me trying to start it up. Stackblitz is so much better these days :/
* Executing task: CodeSandbox: dev yarn run v1.22.19 $ turbo dev Attention: Turborepo now collects completely anonymous telemetry regarding usage. This information is used to shape the Turborepo roadmap and prioritize features. You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL: https://turbo.build/repo/docs/telemetry turbo 2.3.1 WARNING Issues occurred when constructing package graph. Turbo will function, but some features may not be available: × could not resolve workspaces ╰─▶ cannot find binary path: bun • Packages in scope: @repo/eslint-config, @repo/typescript-config, @repo/ui, demo, docs, web • Running dev in 6 packages • Remote caching disabled × unable to find package manager binary: cannot find binary path ╰─▶ cannot find binary path error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. * The terminal process failed to launch (exit code: 1). * Terminal will be reused by tasks, press any key to close it.still doesn’t work for me trying to start it up. Stackblitz is so much better these days :/
* Executing task: CodeSandbox: dev yarn run v1.22.19 $ turbo dev Attention: Turborepo now collects completely anonymous telemetry regarding usage. This information is used to shape the Turborepo roadmap and prioritize features. You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL: https://turbo.build/repo/docs/telemetry turbo 2.3.1 WARNING Issues occurred when constructing package graph. Turbo will function, but some features may not be available: × could not resolve workspaces ╰─▶ cannot find binary path: bun • Packages in scope: @repo/eslint-config, @repo/typescript-config, @repo/ui, demo, docs, web • Running dev in 6 packages • Remote caching disabled × unable to find package manager binary: cannot find binary path ╰─▶ cannot find binary path error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. * The terminal process failed to launch (exit code: 1). * Terminal will be reused by tasks, press any key to close it.Can't it working on codesandbox/stackbiltz. Instead
git clone https://github.com/Apestein/apestein-react-infinite-scroller.gitbun installgit checkout tanstack-query-bugbun run devgo to app called "demo" localhost:3002 Observe the behavior of the infinite scroll container on the right, which is a bi-directional infinite scroll component. It will work as intended (scroll to 49, then scroll back). Now uncomment line 18 in the actions.ts file. Now inverse scrolling will no longer work (scroll to 49 then try to scroll back). The intended behavior is for pageParam 0 to be the last page, meaning 0 is the last number that will appear. Maybe I'm not seeing something but I think this is a bug.sorry this back and forth is a bit pointless for me.
bun installfails with:bun install v1.0.1 (31aec4eb) 🔍 @repo/ui [1/1] error: package "@repo/ui" not found registry.npmjs.org/@repo/ui 404 error: @repo/ui@* failed to resolveand
bun run devfails with:bun run dev $ turbo dev /bin/bash: turbo: command not found error: script "dev" exited with code 127A reproduction for an issue needs to be minimal. Why does it need turbo-repo or packages or anything like that in the first place? Right now, it’s just a waste of my time. I want to see if there is a bug here, but you’re not making it easy for me ...
TkDodo added labelsneeds-info
https://codesandbox.io/p/github/Apestein/tanstack-query-bug-demo/main?import=true
The bug is a result of using bi-directional useInfiniteQuery with maxPages.
Is this a new reproduction of the issue or is this you showing me that it works now? Because I’ve scrolled up and down tons of times and this sandbox works fine. There is also nothing to comment in or out 🤷.
TkDodo closed this
Is this a new reproduction of the issue or is this you showing me that it works now? Because I’ve scrolled up and down tons of times and this sandbox works fine. There is also nothing to comment in or out 🤷.
It's new reproduction. Go to src/app/actions.ts and you will see the comment
when I log:
console.log('hasPreviousPage', hasPreviousPage)in the
BiInfiniteScrollSectioncomponent, I get the correct values. But when I go into your intouseEffectinsideBiInfiniteScroller,hasPreviousPageisfalse.That’s because your effect has missing dependencies, so it creates a stale closure. It’s a common pitfall in react, I’ve written about it here: https://tkdodo.eu/blog/hooks-dependencies-and-stale-closures
There’s a reason react has a linter that enforces exhaustive dependencies.
when I log:
console.log('hasPreviousPage', hasPreviousPage)in the
BiInfiniteScrollSectioncomponent, I get the correct values. But when I go into your intouseEffectinsideBiInfiniteScroller,hasPreviousPageisfalse.That’s because your effect has missing dependencies, so it creates a stale closure. It’s a common pitfall in react, I’ve written about it here: https://tkdodo.eu/blog/hooks-dependencies-and-stale-closures
There’s a reason react has a linter that enforces exhaustive dependencies.
Ok, so this is actually older working version. And I had already solved the closure problem. And even with the correct dependencies, it was still not working. Only figure it out now, that fixing that resulted in another problem with Intersection Observer that again prevented fetching. Basically, if even one px of the intersecting element was on screen, it wouldn't fetch. Don't want you to think I didn't do my due diligence, just unfortunate series of events. Sorry about that.
Sign in to join this conversation.
the sandbox is inaccessible - likely because it’s private or in your drafts. Please make it public