Skip to content

fix(virtualized-lists): keep cell ref callbacks stable across renders - #60247

Open
princeizhere wants to merge 1 commit into
react:mainfrom
princeizhere:fix/virtualizedlist-stable-cell-ref
Open

princeizhere wants to merge 1 commit into
react:mainfrom
princeizhere:fix/virtualizedlist-stable-cell-ref

Conversation

@princeizhere

Copy link
Copy Markdown

Summary:

Was looking into #20174 and checked whether it still reproduces on main. The re-render part doesn't anymore: CellRenderer is a PureComponent and ref isn't part of the props it compares, so cells bail out fine when the list re-renders.

What's still there is the inline ref={ref => { this._cellRefs[key] = ref; }} in _pushCells. Since it's a new function on every render, React detaches the old ref (calls it with null) and attaches the new one for every visible cell on every commit. With 3 cells I counted 6 writes to _cellRefs from a single re-render that didn't change any cell.

This caches the ref callback per cell key and removes it in _onCellUnmount, so the ref only gets attached when a cell mounts. No behavior change otherwise.

Related: #20174

Changelog:

[GENERAL] [CHANGED] - VirtualizedList no longer re-attaches every cell ref on each render

Test Plan:

Added a test to VirtualizedList-test.js that re-renders a list with a prop the cells don't depend on (style) and checks that renderItem isn't called and nothing gets written to _cellRefs. It fails without the change (6 writes) and passes with it.

yarn jest packages/virtualized-lists
Test Suites: 11 passed, 11 total
Tests:       1 skipped, 205 passed, 206 total

yarn flow check, eslint and prettier are clean on the changed files.

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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 23, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant