To recursively scroll a virtualized list until a row has entered the viewport, you need to make use of the scrollToIndex
method provided by the virtualized list library. The scrollToIndex
function accepts an index
argument and an optional align
argument, that determines how the list should be scrolled to the desired row.
One approach to recursively scroll the list to the desired row is to use a recursive function that repeatedly calls scrollToIndex
with an incremented index
argument until the row is in the viewport. Here's an example of how you can implement this:
index.tsx733 chars23 lines
In this example, the scrollToListItem
function takes a virtualized list component and a rowIndex
parameter as arguments. It first checks if the item is already visible in the viewport by comparing the row index with the startIndex
and stopIndex
values of the list's state. If the item is not visible, the function calculates the index to scroll to by adding or subtracting 10 items from the current row index. It then calls the scrollToIndex
function with the calculated index and the start
alignment.
After scrolling to the desired index, the function waits for the list to render the scrolled items using the requestAnimationFrame
method. Once the items are rendered, the function calls itself recursively with the original rowIndex
parameter to check if the row is now visible in the viewport. This process is repeated until the row is visible, after which the function returns.
gistlibby LogSnag