TanStackT
TanStack14mo ago
6 replies
skinny-azure

Keeping previous scroll position when using useWindowVirtualizer

I am currently using the useWindowVirtualizer hook for a large list of about 2000+ items. I am also using
react-router-dom
(but not against migrating to tanstack router but it will be quite a large refactor)

When I scroll down the list and click on any item to go to the single item page and then go and click the browers back button, I want the list to scroll to that item. But if just goes back to the top of the page.

I removed the actual virtualizer part and just renderd it as a pure html ul list and it works as expected, so it must be something to do with the useWindowVirtualizer hook. Im guessing it must be to do with the initialOffest or something.

I have created a really basic example from one of the react-virtuals examples. If you scroll down the page abit and then click on any item it takes you to a random page. Now just click back on the browser, and it will send you back to the top of the page rather than point in the list you were at

https://codesandbox.io/p/devbox/flamboyant-curran-slxj7t

In the past I have used react-virtualized and it didnt have this issue. Ideally I want to stick @tanstack/react-virtual as this does seem like a better package
Was this page helpful?