virtual: Scroll restoration doesn't work when using useWindowVirtualizer
Describe the bug
Scroll restoration doesn’t work when using useWindowVirtualizer.
I used react-virtualized and it works fine.
Your minimal, reproducible example
Steps to reproduce
- scroll down
- click any link
- click to go back.
Expected behavior
The scroll position is restored.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
Chrome
tanstack-virtual version
@tanstack/react-virtual@^3.0.0-beta.18
TypeScript version
No response
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.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 18 (2 by maintainers)
I ended up restoring the scroll position manually. But I don’t need to do this with
react-virtualized.Edit 1: It is not good, even with restoring the scroll position manually.
window.scrollTojust works when@tanstack/virtualwas rendered completely -window.body.scrollHeightis changing when it is rendering. And there is no way to know when @tanstack/virtual is rendered completely.Edit 2: I have restored scroll position by using the option:
initialOffsetI have been trying this (but saving my scroll info in a state store using Zustand instead of history or whatever), and it full on doesn’t work.
useWindowVirtualizerwill sometimes just ignoreinitialOffsetwhen it’s passed. And I can’t figure out any sort of deterministic explanation for when it will honorinitialOffsetand when it won’t.Here’s some code; I’ve confirmed via print debugging that the scroll state is being saved and loaded appropriately, but the virtualizer will then just ignore it, except when it doesn’t. This is in Remix btw. (It’s storing and loading count because I need this to also work with pagination.)
I’ve just stumbled over this issue. I’m using tanstack-router and it doesn’t seem to be solved between these two packages.
For those interested, my resulting, simple, solution is to use the history’s state API. I like it because I don’t have to worry about the scroll restoration being triggered by the wrong screen. Also, no ids!
Works perfectly!
@nuschk great! Thanks for the comment.
@Ugikie or even better you can save it on effect cleanup, checkout this react-router basic example https://stackblitz.com/edit/github-trynzk
@Ugikie that is the case, basic when scroll is restored, virtualizer don’t remember prev read sizes and auto adjustment kiks in, you also need to store
measurementsCacheand restore it by passing via initialMeasurementsCache.for example you can save it on click
@milad1367 This is my code while using next.js I saved the position when users scroll and restore the position by using
scrollToOffset@meotimdihia How did you fix this problem? because when you get the previous scroll position from local storage you need something like offset prop and not initialOffset! can you describe it, please?