Skip to content

Commit

Permalink
Cache ScrollView content length before calling scrollToIndex (faceb…
Browse files Browse the repository at this point in the history
…ook#38736)

Summary:
Pull Request resolved: facebook#38736

`scrollToIndex` relies on cached layout information, so we should cache the results from `onContentSizeChange` before attempting the scroll. Otherwise we will fail to scroll in RTL.

Changelog:
[General][Fixed] Cache ScrollView content length before calling `scrollToIndex`

Reviewed By: lenaic

Differential Revision: D47978635

fbshipit-source-id: 27f2a4702650e8a73e8812128821ca03f36216dd
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Aug 3, 2023
1 parent 90c0e3b commit 33d6da0
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions packages/virtualized-lists/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -1608,9 +1608,32 @@ class VirtualizedList extends StateSafePureComponent<Props, State> {
}

_onContentSizeChange = (width: number, height: number) => {
this._listMetrics.notifyListContentLayout({
layout: {width, height},
orientation: this._orientation(),
});

this._maybeScrollToInitialScrollIndex(width, height);

if (this.props.onContentSizeChange) {
this.props.onContentSizeChange(width, height);
}
this._scheduleCellsToRenderUpdate();
this._maybeCallOnEdgeReached();
};

/**
* Scroll to a specified `initialScrollIndex` prop after the ScrollView
* content has been laid out, if it is still valid. Only a single scroll is
* triggered throughout the lifetime of the list.
*/
_maybeScrollToInitialScrollIndex(
contentWidth: number,
contentHeight: number,
) {
if (
width > 0 &&
height > 0 &&
contentWidth > 0 &&
contentHeight > 0 &&
this.props.initialScrollIndex != null &&
this.props.initialScrollIndex > 0 &&
!this._hasTriggeredInitialScrollToIndex
Expand All @@ -1630,16 +1653,7 @@ class VirtualizedList extends StateSafePureComponent<Props, State> {
}
this._hasTriggeredInitialScrollToIndex = true;
}
if (this.props.onContentSizeChange) {
this.props.onContentSizeChange(width, height);
}
this._listMetrics.notifyListContentLayout({
layout: {width, height},
orientation: this._orientation(),
});
this._scheduleCellsToRenderUpdate();
this._maybeCallOnEdgeReached();
};
}

/* Translates metrics from a scroll event in a parent VirtualizedList into
* coordinates relative to the child list.
Expand Down

0 comments on commit 33d6da0

Please sign in to comment.