Skip to content

Commit

Permalink
Fix VirtualizedList usage of potentially stale state on cell focus
Browse files Browse the repository at this point in the history
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
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Feb 8, 2023
1 parent 1058bb8 commit cab865b
Showing 1 changed file with 1 addition and 12 deletions.
13 changes: 1 addition & 12 deletions packages/virtualized-lists/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -1229,18 +1229,7 @@ class VirtualizedList extends StateSafePureComponent<Props, State> {

_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) => {
Expand Down

0 comments on commit cab865b

Please sign in to comment.