Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
In the official documentation, the height setting is understood as follows: constrowVirtualizer=useVirtualizer({count: hasNextPage ? allRows.length+1 : allRows.length,getScrollElement: ()=>parentRef.current,estimateSize: ()=>300,// item heightoverscan: 4,});If the height of A changes according to the response of the server, is there a way to dynamically change the height? my code: functionVirtualizerScroll(){const{ data, hasNextPage, isFetchingNextPage, fetchNextPage }=useInfiniteQuery(...params);constparentRef=React.useRef<HTMLDivElement>(null);constallRows=data ? data.pages.flatMap((d)=>d.rows) : []constrowVirtualizer=useVirtualizer({count: hasNextPage ? allRows.length+1 : allRows.length,getScrollElement: ()=>parentRef.current,estimateSize: ()=>300,overscan: 4,});constitems=rowVirtualizer.getVirtualItems();React.useEffect(()=>{const[lastItem]=[...rowVirtualizer.getVirtualItems()].reverse();if(!lastItem){return;}if(lastItem.index>=allRows.length-1&&hasNextPage&&!isFetchingNextPage){fetchNextPage();}},[hasNextPage,fetchNextPage,allProducts.length,isFetchingNextPage,rowVirtualizer.getVirtualItems(),]);return(<divref={parentRef}style={{height: 1000,width: '100%',overflowY: 'auto',}}><divstyle={{height: rowVirtualizer.getTotalSize(),width: '100%',position: 'relative',}}>{items.map((virtualRow)=>{constresponse=allRows[virtualRow.index];return(<divkey={virtualRow.key}data-index={virtualRow.index}ref={rowVirtualizer.measureElement}style={{position: 'absolute',top: 0,left: 0,width: '100%',height: `${virtualRow.size}px`,transform: `translateY(${virtualRow.start}px)`,}}><DynamicCardresponse={response}/></div>);})}</div></div>);}I figure out various ways but can't find a way. I was wondering if anyone could give me some guidance or an example. |
Answered by
zi-gae
Jan 20, 2023
Replies: 2 comments 1 reply
I found the answer |
1 reply
Answer selected byzi-gae
The answer is outdated. Could anyone provide a solution for the latest version, please? |
0 replies
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
I found the answer
https://tanstack.com/virtual/v3/docs/examples/react/dynamic