Skip to content
Source
Sign inSign up
TanStack/query

Bi-directional infinite query will not fetch previous page if initial prevCursor is null #8330

Send to my agent
ClosedApestein(GitHub profile, opens in a new tab) opened this issue 10 comments

Describe 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

  1. the sandbox is inaccessible - likely because it’s private or in your drafts. Please make it public

  2. the sandbox is inaccessible - likely because it’s private or in your drafts. Please make it public

    my bad, it's fixed

  3. 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. 
  4. 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.git bun install git checkout tanstack-query-bug bun run dev go 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.

  5. sorry this back and forth is a bit pointless for me. bun install fails 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 resolve

    and bun run dev fails with:

    bun run dev
    $ turbo dev
    /bin/bash: turbo: command not found
    error: script "dev" exited with code 127

    A 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 ...

  6. TkDodo added labelsneeds-info

  7. 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.

  8. 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 🤷.

  9. TkDodo closed this

  10. 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

  11. when I log:

    console.log('hasPreviousPage', hasPreviousPage)

    in the BiInfiniteScrollSection component, I get the correct values. But when I go into your into useEffect inside BiInfiniteScroller, hasPreviousPage is false.

    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.

  12. when I log:

    console.log('hasPreviousPage', hasPreviousPage)

    in the BiInfiniteScrollSection component, I get the correct values. But when I go into your into useEffect inside BiInfiniteScroller, hasPreviousPage is false.

    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.