Skip to content

Commit 62a0640

Browse files
NickGerlemanfacebook-github-bot
authored andcommittedMar 1, 2023
Avoid VirtualizedList viewability updates during state updates
Summary: VirtualizedList refactoring moved [a call of `_updateViewableItems`](https://www.internalfb.com/code/fbsource/[a9d4ad3cf149][history][blame]/xplat/js/react-native-github/Libraries/Lists/VirtualizedList.js?lines=1431-1447) to the inside of a state update. This call may trigger an `onViewableItemsChanged`, which is normally not an issue (minus changing timing), but creates problems if the users callback then calls imperative methods on the VirtualizedList, since the batched state update may be in the process of changing the props/state the imperative method is reading. See #36329 for what I suspect is an example of this. This moves the `_updateViewableItems` call to before the state update, like the previous version of VirtualizedList. Changelog: [General][Fixed] - Avoid VirtualizedList viewability updates during state updates Reviewed By: javache Differential Revision: D43665606 fbshipit-source-id: 9398273c5209e371e69aafb02bac173c69874273
1 parent cbc279c commit 62a0640

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed
 

‎packages/virtualized-lists/Lists/VirtualizedList.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,6 @@ class VirtualizedList extends StateSafePureComponent<Props, State> {
554554
const onEndReachedThreshold = onEndReachedThresholdOrDefault(
555555
props.onEndReachedThreshold,
556556
);
557-
this._updateViewableItems(props, cellsAroundViewport);
558-
559557
const {contentLength, offset, visibleLength} = this._scrollMetrics;
560558
const distanceFromEnd = contentLength - visibleLength - offset;
561559

@@ -1714,6 +1712,8 @@ class VirtualizedList extends StateSafePureComponent<Props, State> {
17141712
};
17151713

17161714
_updateCellsToRender = () => {
1715+
this._updateViewableItems(this.props, this.state.cellsAroundViewport);
1716+
17171717
this.setState((state, props) => {
17181718
const cellsAroundViewport = this._adjustCellsAroundViewport(
17191719
props,

0 commit comments

Comments
 (0)