question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use scrollToItem with InfiniteLoader?

See original GitHub issue

Hi,

I’m a little confused about how to call scrollToItem() when the VariableSizedList is within an InfiniteLoader tag. My understanding is that you would use createRef() to assign a new ref to the list, but there is already a ref getting passed down. I’ve tried running scrollToItem on this latter ref, but it doesn’t work:

	return (
		<InfiniteLoader
  			isItemLoaded={isItemLoaded}
  			itemCount={itemCount}
  			loadMoreItems={loadMoreItems}
		>
		  {({ onItemsRendered, ref }) => {
			if (scrollTargetBlockIndex>0) {
				ref.current.scrollToItem(scrollTargetBlockIndex);
				scrollToConsumerSuccess();
			};
		  	return (
 			<AutoSizer>
			{
				({height, width}) => (
					<VariableSizeList
				    	height={height}
				    	width={width}
				    	itemCount={consumerBlocks.length}
				    	itemSize={getItemSize}
				    	onItemsRendered={onItemsRendered}
				    	ref={ref}
				    	overscanCount={4}
				    	minimumBatchSize={20}
				  	>
				    {Row}
				   </VariableSizeList>
				)
			}
			</AutoSizer>
		  )}}
		</InfiniteLoader>
	);

The key line is


ref.current.scrollToItem(scrollTargetBlockIndex);

How should I do this? Is it possible?

Thank you in advance.

Simon

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

8reactions
danielcrusercommented, May 8, 2020

@manacoa I just dealt with something like this myself.

What worked for me was

const Component = () => {
    // bunch of code 
    const virtualLoaderRef = useRef(null);

    const customHandleScroll = ({ visibleStartIndex }) => {
        if (// logic for whether to trigger scroll) {
             virtualLoaderRef.current._listRef.scrollToItem(indexYouWantToScrollTo)
        }
     }

   return (
      <InfiniteLoader
        ref={virtualLoaderRef}
        isItemLoaded={isItemLoaded}
        itemCount={totalCount} 
        loadMoreItems={loadMoreItems}
        threshold={threshold}
      >
        {({ onItemsRendered, ref }) => (
          <VariableSizeList
            onItemsRendered={({
              overscanStartIndex,
              overscanStopIndex,
              visibleStartIndex,
              visibleStopIndex
            }) => {
              onItemsRendered({
                overscanStartIndex,
                overscanStopIndex,
                visibleStartIndex,
                visibleStopIndex
              });
              customHandleScroll({ visibleStartIndex });
            }}
            ref={ref}
            height={800}
            itemCount={totalCount}
            itemSize={getItemSize}
            itemData={itemData}
            width={300}
            itemKey={itemKey}
          >
            {Row}
          </VariableSizeList>
        )}
      </InfiniteLoader>
  )
}

key line being virtualLoaderRef.current._listRef.scrollToItem(indexYouWantToScrollTo) in conjunction with setting the ref on InfiniteLoader

Hope that’s helpful

3reactions
johnnyoshikacommented, Jun 9, 2020

It would be nice if _listRef was exposed publicly.

@bvaughn had an alternative suggestion here: https://github.com/bvaughn/react-window/issues/324#issuecomment-528887341

His proposed solution doesn’t work for me, however, as it results in a whole bunch of typescript errors, so I’ll use _listRef for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scroll to specific value - javascript - Stack Overflow
I'm using InfiniteLoader with FixedSizeGrid from bvaughn/react-window and I'm ... scrollToItem(20); }; <button className="ExampleButton" ...
Read more >
Storing and Restoring Scroll Position with React Window
To restore the user to their previous scroll location, create a bit of state storing the user's most recent scroll location, then navigate ......
Read more >
react-simple-infinite-loading v1 is out | Yvonnick Frin
import React from 'react'. import InfiniteLoading from 'react-simple-infinite-loading'. function Example({ items, fetchMore, hasMore }) {.
Read more >
scrollToItem(at:at:animated:) | Apple Developer Documentation
scrollToItem (at:at:animated:) Scrolls the collection view contents until the specified item is visible. iOS 6.0+ iPadOS 6.0+ Mac Catalyst 13.0+ tvOS 9.0+ ...
Read more >
react-window-infinite-loader examples - CodeSandbox
Learn how to use react-window-infinite-loader by viewing and forking react-window-infinite-loader example apps on CodeSandbox.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found