TanStack / TanStack/virtual

[VR] Scrolling to index is incorrectly align for first effect in dynamic mode

Open
#615 1 comment 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
7.1k
Forks
466
Avg merge
2d 31m
Merged PRs (30d)
13

Description

Describe the bug

When calling scrolling to index in initial render the item is not correctly align,

React.useEffect(() => {
  virtualizer.scrollToIndex(500)
}, [virtualizer])

it happens because in dynamic mode position of item can change while elements are mounted. To fix this, we are checking in next tick if the position changed. Overall this should be only enabled for dynamic case, via isDynamicMode condition https://github.com/TanStack/virtual/blob/beta/packages/virtual-core/src/index.ts#L826 and there we only check if we have rendered elements.

This was working as side effect of rendering rows even when we should not, noticed after #603.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/recursing-kowalevski-dfl62x

Steps to reproduce

No action required.

Expected behavior

Item is correctly align.

How often does this bug happen?

Every time

Screenshots or Videos

.

Platform

macOS, Chrome

tanstack-virtual version

v3.0.0-beta.68

TypeScript version

No response

Additional context

Current workaround wound be to wrap the scrollToIndex with setTimeout

React.useEffect(() => {
  setTimeout(() => {
    virtualizer.scrollToIndex(500)
  }, 0)
}, [virtualizer])
Terms & Code of Conduct
  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in packages/virtual-core/src/index.ts around line 826 and reproduce the initial scrollToIndex(500) behavior using the linked CodeSandbox. Inspect the dynamic-mode and rendered-elements checks described in the issue. Done means the item is correctly aligned on the initial render without requiring the setTimeout workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend, performance
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.