Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions packages/virtualized-lists/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1468,6 +1466,11 @@ class VirtualizedList extends StateSafePureComponent<
}

_cellRefs: {[key: string]: null | CellRenderer<any>} = {};
// 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<string, (ref: null | CellRenderer<any>) => void> =
new Map();
_fillRateHelper: FillRateHelper;
_listMetrics: ListMetricsAggregator = new ListMetricsAggregator();
_footerLength = 0;
Expand Down Expand Up @@ -1601,8 +1604,22 @@ class VirtualizedList extends StateSafePureComponent<
}
};

_getCellRefCallback(
cellKey: string,
): (ref: null | CellRenderer<any>) => 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);
};

Expand Down
42 changes: 42 additions & 0 deletions packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}) => <item value={item.key} />);
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(
<VirtualizedList ref={listRef} {...props} style={{opacity: 1}} />,
);
});

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(
<VirtualizedList ref={listRef} {...props} style={{opacity: 0.5}} />,
);
});

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 <item value={item.key} />;
Expand Down