|
| 1 | +import React from 'react' |
| 2 | +import ReactDOM from 'react-dom/client' |
| 3 | +import { useVirtualizer } from '@tanstack/react-virtual' |
| 4 | + |
| 5 | +/** |
| 6 | + * Regression test app for stale index bug: |
| 7 | + * When items are removed from the end of the list, the ResizeObserver may fire |
| 8 | + * for a disconnected node whose data-index >= the new count. If getItemKey |
| 9 | + * indexes into the items array, this causes an out-of-bounds error. |
| 10 | + */ |
| 11 | + |
| 12 | +interface Item { |
| 13 | + id: string |
| 14 | + label: string |
| 15 | +} |
| 16 | + |
| 17 | +function makeItems(count: number): Array<Item> { |
| 18 | + return Array.from({ length: count }, (_, i) => ({ |
| 19 | + id: `item-${i}`, |
| 20 | + label: `Row ${i}`, |
| 21 | + })) |
| 22 | +} |
| 23 | + |
| 24 | +const App = () => { |
| 25 | + const parentRef = React.useRef<HTMLDivElement>(null) |
| 26 | + const [items, setItems] = React.useState(() => makeItems(20)) |
| 27 | + const [error, setError] = React.useState<string | null>(null) |
| 28 | + |
| 29 | + const rowVirtualizer = useVirtualizer({ |
| 30 | + count: items.length, |
| 31 | + getScrollElement: () => parentRef.current, |
| 32 | + estimateSize: () => 50, |
| 33 | + getItemKey: (index) => { |
| 34 | + if (index < 0 || index >= items.length) { |
| 35 | + const msg = `getItemKey called with stale index ${index} (count=${items.length})` |
| 36 | + setError(msg) |
| 37 | + throw new Error(msg) |
| 38 | + } |
| 39 | + return items[index].id |
| 40 | + }, |
| 41 | + }) |
| 42 | + |
| 43 | + const removeLastFive = () => { |
| 44 | + setItems((prev) => prev.slice(0, Math.max(0, prev.length - 5))) |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <div> |
| 49 | + <button data-testid="remove-items" onClick={removeLastFive}> |
| 50 | + Remove last 5 |
| 51 | + </button> |
| 52 | + <div data-testid="item-count">Count: {items.length}</div> |
| 53 | + {error && <div data-testid="error">{error}</div>} |
| 54 | + <div |
| 55 | + ref={parentRef} |
| 56 | + data-testid="scroll-container" |
| 57 | + style={{ height: 300, overflow: 'auto' }} |
| 58 | + > |
| 59 | + <div |
| 60 | + style={{ |
| 61 | + height: rowVirtualizer.getTotalSize(), |
| 62 | + position: 'relative', |
| 63 | + }} |
| 64 | + > |
| 65 | + {rowVirtualizer.getVirtualItems().map((v) => { |
| 66 | + const item = items[v.index] |
| 67 | + return ( |
| 68 | + <div |
| 69 | + key={item.id} |
| 70 | + data-testid={item.id} |
| 71 | + ref={rowVirtualizer.measureElement} |
| 72 | + data-index={v.index} |
| 73 | + style={{ |
| 74 | + position: 'absolute', |
| 75 | + top: 0, |
| 76 | + left: 0, |
| 77 | + transform: `translateY(${v.start}px)`, |
| 78 | + width: '100%', |
| 79 | + height: 50, |
| 80 | + borderBottom: '1px solid #ccc', |
| 81 | + padding: 8, |
| 82 | + boxSizing: 'border-box', |
| 83 | + }} |
| 84 | + > |
| 85 | + {item.label} |
| 86 | + </div> |
| 87 | + ) |
| 88 | + })} |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + ) |
| 93 | +} |
| 94 | + |
| 95 | +ReactDOM.createRoot(document.getElementById('root')!).render(<App />) |
0 commit comments