From ccb59dd1ead653705e237e344d68ef295848cf5e Mon Sep 17 00:00:00 2001 From: Prince Kumar Date: Tue, 24 Nov 2026 01:49:00 +0530 Subject: [PATCH] fix(virtualized-lists): keep cell ref callbacks stable across renders VirtualizedList created a new inline ref callback for every cell on each render, so React detached and re-attached every CellRenderer ref on each commit even when the cells themselves bailed out of rendering. Cache the callback per cell key and drop it when the cell unmounts. --- .../Lists/VirtualizedList.js | 23 ++++++++-- .../Lists/__tests__/VirtualizedList-test.js | 42 +++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/packages/virtualized-lists/Lists/VirtualizedList.js b/packages/virtualized-lists/Lists/VirtualizedList.js index b58f45976f1c..e9655f64a8dc 100644 --- a/packages/virtualized-lists/Lists/VirtualizedList.js +++ b/packages/virtualized-lists/Lists/VirtualizedList.js @@ -1016,9 +1016,7 @@ class VirtualizedList extends StateSafePureComponent< onUpdateSeparators={this._onUpdateSeparators} onCellFocusCapture={this._onCellFocusCapture} onUnmount={this._onCellUnmount} - ref={ref => { - this._cellRefs[key] = ref; - }} + ref={this._getCellRefCallback(key)} renderItem={renderItem} {...(shouldListenForLayout && { onCellLayout: this._onCellLayout, @@ -1468,6 +1466,11 @@ class VirtualizedList extends StateSafePureComponent< } _cellRefs: {[key: string]: null | CellRenderer} = {}; + // Ref callbacks are cached per cell key so that re-rendering the list does + // not hand every cell a new ref function, which would make React detach and + // re-attach each cell's ref on every commit. + _cellRefCallbacks: Map) => void> = + new Map(); _fillRateHelper: FillRateHelper; _listMetrics: ListMetricsAggregator = new ListMetricsAggregator(); _footerLength = 0; @@ -1601,8 +1604,22 @@ class VirtualizedList extends StateSafePureComponent< } }; + _getCellRefCallback( + cellKey: string, + ): (ref: null | CellRenderer) => void { + let callback = this._cellRefCallbacks.get(cellKey); + if (callback == null) { + callback = ref => { + this._cellRefs[cellKey] = ref; + }; + this._cellRefCallbacks.set(cellKey, callback); + } + return callback; + } + _onCellUnmount = (cellKey: string) => { delete this._cellRefs[cellKey]; + this._cellRefCallbacks.delete(cellKey); this._listMetrics.notifyCellUnmounted(cellKey); }; diff --git a/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js b/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js index 5bc0254c7127..df0c9938612c 100644 --- a/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js +++ b/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js @@ -46,6 +46,48 @@ describe('VirtualizedList', () => { expect(component).toMatchSnapshot(); }); + it('keeps cell refs stable when the list re-renders', async () => { + const listRef = createRef(); + const renderItem = jest.fn(({item}) => ); + const props = { + data: [{key: 'i1'}, {key: 'i2'}, {key: 'i3'}], + renderItem, + getItem: (data, index) => data[index], + getItemCount: data => data.length, + }; + let component; + await act(() => { + component = create( + , + ); + }); + + const cellRefs = listRef.current._cellRefs; + expect(Object.keys(cellRefs)).toEqual(['i1', 'i2', 'i3']); + const refWrites = jest.fn(); + listRef.current._cellRefs = new Proxy(cellRefs, { + set(target, key, value) { + refWrites(key, value); + target[key] = value; + return true; + }, + }); + renderItem.mockClear(); + + // Re-render the list with a prop that doesn't affect the cells. + await act(() => { + component.update( + , + ); + }); + + expect(renderItem).not.toHaveBeenCalled(); + expect(refWrites).not.toHaveBeenCalled(); + for (const key of ['i1', 'i2', 'i3']) { + expect(listRef.current._cellRefs[key]).not.toBeNull(); + } + }); + it('renders simple list using ListItemComponent', async () => { function ListItemComponent({item}) { return ;