petyosi / react-virtuoso

The most powerful virtual list component for React
https://virtuoso.dev
MIT License
5.25k stars 301 forks source link

Mount `VirtuosoGrid` scrolled to a specific index #350

Closed itayganor closed 3 years ago

itayganor commented 3 years ago

Hi!

I'm trying to make my VirtuosoGrid mount scrolled to a specific index. I've seen the regular Virtuoso has a initialTopMostItemIndex prop, which VirtuosoGrid does not have.

I also tried to scroll on mount:

useEffect(() => {
    virtuoso.ref.scrollToIndex({index: 40, behavior: 'smooth'});
}, []);

But it doesn't seem to work very well - my guess is that the grid hasn't finished calculating its heights yet.

What's the correct way of doing so?

Thank you! <3

petyosi commented 3 years ago

Your statement is correct - setting the initial location is not supported in the grid. Wrapping the call from above in a setTimeout(, 100) should work I think.

itayganor commented 3 years ago

Your statement is correct - setting the initial location is not supported in the grid. Wrapping the call from above in a setTimeout(, 100) should work I think.

@petyosi it means you'll end up having a flash of wrong scroll before you actually scroll to the desired index.

Is there a reason why initialTopMostItemIndex is not supported? Will it be available in the future?

petyosi commented 3 years ago

I am aware of the shortcomings of the workaround I suggested. That's why it's a workaround and not an official solution. There's no technical reason to not support initialTopMostItemIndex. It is possible and relatively straightforward to do so, but it needs a good day or so of someones' time.

To make this clear: React Virtuoso is an open-source project which I develop, release for free, and expect nothing in return. Some people contribute to it, which is awesome, and I do my best to help. Others chose to sponsor the project in general or sponsor the development of certain features - which is also something I can accommodate if the feature is aligned with my direction of the project.

This being said, if you are willing to contribute to the project or have your company sponsor its development, please let me know.