Repository navigation
fix(virtualized-lists): keep cell ref callbacks stable across renders - #60247
Open
princeizhere wants to merge 1 commit into
Open
princeizhere wants to merge 1 commit into
princeizhere wants to merge 1 commit into
Conversation
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Was looking into #20174 and checked whether it still reproduces on main. The re-render part doesn't anymore:
CellRendereris aPureComponentandrefisn'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 withnull) and attaches the new one for every visible cell on every commit. With 3 cells I counted 6 writes to_cellRefsfrom 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.jsthat re-renders a list with a prop the cells don't depend on (style) and checks thatrenderItemisn't called and nothing gets written to_cellRefs. It fails without the change (6 writes) and passes with it.yarn flow check, eslint and prettier are clean on the changed files.