From cab865be797b724d2fda5441e0ef23559180f722 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 7 Feb 2023 18:25:28 -0800 Subject: [PATCH] Fix VirtualizedList usage of potentially stale state on cell focus Summary: State updates can be batched together idependent of `this.state`, so we should do any calculation deriving state from state within a `setState()` callback. This fixes a bug where we were relying on potentially stale state, a RenderMask derived from `this.state` instead of the `state` callback parameter, when triggering updates from focus. Note that this is not exercised on Android/iOS, but it on desktop/web. I noticed this a while back while making another change, but that change got abandoned, so this is the independent fix. Changelog: [General][Fixed] - Calculate VirtualizedList render mask for focused cell during batched state updates Reviewed By: javache Differential Revision: D43073415 fbshipit-source-id: dee4197ec925a6d8d427b63fb063aa4e3b58c595 --- packages/virtualized-lists/Lists/VirtualizedList.js | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/packages/virtualized-lists/Lists/VirtualizedList.js b/packages/virtualized-lists/Lists/VirtualizedList.js index b93878c5658295..638daf14895558 100644 --- a/packages/virtualized-lists/Lists/VirtualizedList.js +++ b/packages/virtualized-lists/Lists/VirtualizedList.js @@ -1229,18 +1229,7 @@ class VirtualizedList extends StateSafePureComponent { _onCellFocusCapture(cellKey: string) { this._lastFocusedCellKey = cellKey; - const renderMask = VirtualizedList._createRenderMask( - this.props, - this.state.cellsAroundViewport, - this._getNonViewportRenderRegions(this.props), - ); - - this.setState(state => { - if (!renderMask.equals(state.renderMask)) { - return {renderMask}; - } - return null; - }); + this._updateCellsToRender(); } _onCellUnmount = (cellKey: string) => {