When an item is removed from a virtualized list, the removed element's ResizeObserver callback can fire with a stale data-index that collides with a surviving element's new index. _measureElement misidentifies the dead element as the live one, swaps the elementsCache entry (unobserving the live element, observing the dead one), and the live element is permanently orphaned from observation. Any subsequent size changes to that element are invisible to the virtualizer.
Affected version: @tanstack/[email protected] (@latest as of February 2025) and likely all earlier versions with the same _measureElement logic
broken/— Reproduces the bug using unpatched@tanstack/[email protected](@latest)fixed/— Same app with apatch-packagefix applied to@tanstack/virtual-core
Each subdirectory is an independent Vite + React + TypeScript app.
# Run the broken version
cd broken
npm install
npm run dev
# Run the fixed version (patch-package applies automatically)
cd fixed
npm install
npm run dev- Expand A — click "Expand A". A's row grows to ~160px. The virtualizer updates
totalSize. Works fine. - Collapse A — click "Collapse A". A shrinks back. Works fine.
- Delete A — click "Delete A". A is removed. B shifts from index 1 to index 0.
- Expand B — click "Expand B". B's row should grow, but
totalSizestays at ~35px. BUG.
The totalSize indicator at the top turns red when the virtualizer's size doesn't match the expanded content.
Click "Reset" to restore the initial state and try again.
In Virtualizer._measureElement (@tanstack/virtual-core/dist/esm/index.js):
this._measureElement = (node, entry) => {
const index = this.indexFromElement(node); // reads data-index from DOM
const item = this.measurementsCache[index];
if (!item) return;
const key = item.key;
const prevNode = this.elementsCache.get(key);
if (prevNode !== node) {
if (prevNode) this.observer.unobserve(prevNode); // ← unobserves LIVE element
this.observer.observe(node); // ← observes DEAD element
this.elementsCache.set(key, node); // ← maps live key → dead element
}
if (node.isConnected) { // ← too late, damage done
this.resizeItem(index, this.options.measureElement(node, entry, this));
}
};When item A (index 0) is deleted:
- B shifts from index 1 to index 0
- A's dead div fires a final ResizeObserver callback (height → 0)
_measureElementreadsdata-index=0from A's dead divmeasurementsCache[0]is now B's entry- The method thinks A's dead div is B's div → swaps the observer
- B's real div is unobserved forever
Check node.isConnected before the elementsCache swap:
this._measureElement = (node, entry) => {
if (!node.isConnected) {
this.observer.unobserve(node);
return;
}
const index = this.indexFromElement(node);
const item = this.measurementsCache[index];
if (!item) return;
const key = item.key;
const prevNode = this.elementsCache.get(key);
if (prevNode !== node) {
if (prevNode) this.observer.unobserve(prevNode);
this.observer.observe(node);
this.elementsCache.set(key, node);
}
this.resizeItem(index, this.options.measureElement(node, entry, this));
};The patch is applied in fixed/patches/.