useWindowVirtualizer with measureElement breaks scroll restoration #997
Describe the bug#
When using "@tanstack/react-virtual": "3.13.8" and "@tanstack/react-router": "1.112.12"
If the page contains a virtualized list like this:
const virtualizer = useWindowVirtualizer({
gap: 16,
overscan: 4,
count: data.items.length,
estimateSize: constant(188),
scrollMargin: listRef.current?.offsetTop ?? 0,
});
And the items use:
ref={virtualizer.measureElement}
data-index={index}
But the actual element sizes differ from estimateSize, triggering re-measurement on mount — in that case, scrollRestoration breaks.
This might be an issue on the @tanstack/react-router side; I’ll duplicate this issue there as well.
Current workaround: add this option to the virtualizer:
scrollToFn: () => null,
Your minimal, reproducible example#
https://codesandbox.io/p/sandbox/2lryyk
Steps to reproduce#
1 Open the Home page 2 Scroll down a bit 3 Navigate to the List page 4 You'll notice that the page is not scrolled to the top
In the code, there's a commented line:
in code there are commented string with
scrollToFn: () => null
Uncommenting this line. After doing that, navigation with scroll restoration works perfectly.
Expected behavior#
Virtualizer does not break scroll restoration.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
maxOS
tanstack-virtual version#
3.13.8
TypeScript version#
5.7.3
Additional context#
No response
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.
Duplicate issue in router repo - https://github.com/TanStack/router/issues/4107
Just ran into this issue today. My project doesn't use
tanstack/react-router, so I believe this issue is independent of routing and might be better fixed in the virtualization implementation itself.For context, I also have dynamically sized elements managed by a WindowVirtualizer, and the
scrollToFn: () => nullworkaround fixed the issue.I am having the same issue with
useVirtualizerand measureElement using Vue.js & vue-router.I can reproduce this on the current
main. The cause is the defaultinitialOffsetofuseWindowVirtualizer, which is() => window.scrollY.After a client-side route change, the first render can read the scroll position of the previous page. The virtualizer then writes this position back to the window when it attaches. It also writes it again when it measures rows above this position. This overrides the scroll reset or the scroll restore of the router.
Workaround: set
initialOffsetexplicitly.useWindowVirtualizer({ count, estimateSize: () => 188, initialOffset: 0, // or the position that the router restores })This is better than
scrollToFn: () => null, because that also disablesscrollToIndexand the size corrections.There is an open PR, #1044, that changes the default.
Sign in to join this conversation.
as far as I understand the problem is that the virtualizer tries to keep visible elements in their places and when recalculating the height - it moves the scroll container, in the case of useWindowVirtualizer - the entire body.
And here the question is rather why the value of the previous page is taken as the initial value of the scroll position. At what point does the magic of scroll restoration occur.