|
| 1 | +import React from 'react' |
| 2 | +import ReactDOM from 'react-dom' |
| 3 | + |
| 4 | +import './index.css' |
| 5 | + |
| 6 | +import { useMeasure } from '@react-hookz/web/esm' |
| 7 | +import { useVirtualizer } from '@tanstack/react-virtual' |
| 8 | + |
| 9 | +function App() { |
| 10 | + const parentRef = React.useRef<HTMLDivElement>() |
| 11 | + const [theadSize, theadRef] = useMeasure<HTMLTableSectionElement>() |
| 12 | + |
| 13 | + const rowVirtualizer = useVirtualizer({ |
| 14 | + count: 10000, |
| 15 | + getScrollElement: () => parentRef.current, |
| 16 | + estimateSize: React.useCallback(() => 35, []), |
| 17 | + overscan: 5, |
| 18 | + paddingStart: theadSize?.height ?? 0, |
| 19 | + scrollPaddingStart: theadSize?.height ?? 0, |
| 20 | + }) |
| 21 | + |
| 22 | + return ( |
| 23 | + <> |
| 24 | + <div className="flex gap-2"> |
| 25 | + <button |
| 26 | + onClick={() => { |
| 27 | + rowVirtualizer.scrollToIndex(40) |
| 28 | + }} |
| 29 | + className="border border-black" |
| 30 | + > |
| 31 | + Scroll to index 40 |
| 32 | + </button> |
| 33 | + <button |
| 34 | + onClick={() => { |
| 35 | + rowVirtualizer.scrollToIndex(20) |
| 36 | + }} |
| 37 | + className="border border-black" |
| 38 | + > |
| 39 | + Then scroll to index 20 |
| 40 | + </button> |
| 41 | + </div> |
| 42 | + |
| 43 | + <br /> |
| 44 | + <br /> |
| 45 | + |
| 46 | + <div |
| 47 | + ref={parentRef} |
| 48 | + className="List" |
| 49 | + style={{ |
| 50 | + height: `200px`, |
| 51 | + width: `400px`, |
| 52 | + overflow: 'auto', |
| 53 | + }} |
| 54 | + > |
| 55 | + <table |
| 56 | + style={{ |
| 57 | + height: `${rowVirtualizer.getTotalSize()}px`, |
| 58 | + width: '100%', |
| 59 | + }} |
| 60 | + > |
| 61 | + <thead ref={theadRef}> |
| 62 | + <tr> |
| 63 | + <th>Index</th> |
| 64 | + <th>Key</th> |
| 65 | + </tr> |
| 66 | + </thead> |
| 67 | + <tbody> |
| 68 | + {rowVirtualizer.getVirtualItems().map((virtualRow) => ( |
| 69 | + <tr |
| 70 | + key={virtualRow.index} |
| 71 | + className={ |
| 72 | + virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven' |
| 73 | + } |
| 74 | + style={{ |
| 75 | + position: 'absolute', |
| 76 | + top: 0, |
| 77 | + left: 0, |
| 78 | + width: '100%', |
| 79 | + height: `${virtualRow.size}px`, |
| 80 | + transform: `translateY(${virtualRow.start}px)`, |
| 81 | + }} |
| 82 | + > |
| 83 | + <td>#{virtualRow.index}</td> |
| 84 | + <td>{virtualRow.key}</td> |
| 85 | + </tr> |
| 86 | + ))} |
| 87 | + </tbody> |
| 88 | + </table> |
| 89 | + </div> |
| 90 | + {process.env.NODE_ENV === 'development' ? ( |
| 91 | + <p> |
| 92 | + <strong>Notice:</strong> You are currently running React in |
| 93 | + development mode. Rendering performance will be slightly degraded |
| 94 | + until this application is build for production. |
| 95 | + </p> |
| 96 | + ) : null} |
| 97 | + </> |
| 98 | + ) |
| 99 | +} |
| 100 | + |
| 101 | +ReactDOM.render( |
| 102 | + <React.StrictMode> |
| 103 | + <App /> |
| 104 | + </React.StrictMode>, |
| 105 | + document.getElementById('root'), |
| 106 | +) |
0 commit comments