You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have troubles combining dynamically sized items with scrollToIndex().
Every item within my virtualized list can be expanded. Thus, I'm using measureElement to dynamically measure the height of each entry.
However, I would like to combine it with a smooth scrollToIndex method. This is not working. But iff I don't pass the scrollToFn function to useVirtualizer, rowVirtualizer.scrollToIndex() works but obviously withouth smooth scroll animation.
Can someone please guide me what I could possibly do wrong?
Thanks!
constscrollToFn: VirtualizerOptions<HTMLDivElement,HTMLDivElement>['scrollToFn']=React.useCallback((offset,canSmooth,instance)=>{constduration=1000conststart=parentRef.current?.scrollTop||0conststartTime=(scrollingRef.current=Date.now())construn=()=>{if(scrollingRef.current!==startTime)returnconstnow=Date.now()constelapsed=now-startTimeconstprogress=easeInOutQuint(Math.min(elapsed/duration,1))constinterpolated=start+(offset-start)*progressif(elapsed<duration){elementScroll(interpolated,canSmooth,instance)requestAnimationFrame(run)}else{elementScroll(interpolated,canSmooth,instance)}}requestAnimationFrame(run)},[])constrowVirtualizer=useVirtualizer<HTMLDivElement,HTMLDivElement>({count: allRows.length,getScrollElement: ()=>parentRef.current,estimateSize: ()=>100,initialOffset: expandedIndex ? expandedIndex*100+10*expandedIndex : 0,overscan: 5,gap: 10,//scrollToFn -> if I pass this function, then rowVirtualizer.scrollToIndex(randomIndex, { align: 'start' }) does not work})return(<divclassName='flex flex-1 flex-col min-h-full w-full'><div><buttononClick={()=>{rowVirtualizer.scrollToIndex(randomIndex,{align: 'start'})}}>ScrollToRandomIndex({randomIndex})</button></div><AutoSizer>{({ height, width }: {height: number;width: number})=>(<divref={parentRef}className='w-full h-full no-scrollbar'style={{height: height,width: width,overflow: 'auto'}}><divstyle={{height: `${rowVirtualizer.getTotalSize()}px`,width: '100%',position: 'relative'}}>{rowVirtualizer.getVirtualItems().map((virtualRow)=>{return(<divkey={virtualRow.index}data-index={virtualRow.index}ref={rowVirtualizer.measureElement}// -> this is important for measuring dynamically changing the size of the itemclassName='flex flex-col'style={{position: 'absolute',top: 0,left: 0,width: '100%',// height: `${virtualRow.size}px`,transform: `translateY(${virtualRow.start}px)`
}}><Rowindex={virtualRow.index}item={allRows[virtualRow.index]}/></div>)})}</div></div>)}</AutoSizer></div>)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I have troubles combining dynamically sized items with scrollToIndex().
Every item within my virtualized list can be expanded. Thus, I'm using measureElement to dynamically measure the height of each entry.
However, I would like to combine it with a smooth scrollToIndex method. This is not working. But iff I don't pass the scrollToFn function to useVirtualizer, rowVirtualizer.scrollToIndex() works but obviously withouth smooth scroll animation.
Can someone please guide me what I could possibly do wrong?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions