How to get current `topMostItemindex` for `react-virtuoso`?

759 Views Asked by At

I want to persist the scroll position of my <Virtuoso /> list through unmounts/remounts.

I think I want to use the initialTopMostItemIndex property to restore my previous scroll position in order to achieve this - this post makes me believe this is better than scrolling imperatively post-render.

But how can I get the current topMostItemIndex, so I can save it and restore it later?

I use Virtuoso in a manner similar to this playground:

<Virtuoso
   style={{ height: 400 }}
   data={generateUsers(1000)}
   ref={ref}
   itemContent={(index, user) => (
      <div>
        <h4>
            {user.index}. {user.name}
        </h4>
        <div style={{ marginTop: "1rem" }}>{user.description}</div>
      </div>
   )}
/>
1

There are 1 best solutions below

0
On

Recently I added a method called getState that is paired with the restoreStateFrom property.

Check the example.