Skip to content

Commit d3658bc

Browse files
MartinSherburnfacebook-github-bot
authored andcommitted
Fix issue with onEndReached
Summary: onEndReached can be triggered twice when more items are added to the end of the list. This change makes it so that a second call to onEndReached won't happen until the user scrolls down to the new end of the list. Changelog: [General] [Fixed] - Fix double call to onEndReached in VirtualizedList Reviewed By: sahrens Differential Revision: D20066740 fbshipit-source-id: 129d7ae6bfd241eeea18fe0bb12b82be67735874
1 parent d2f314a commit d3658bc

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

Libraries/Lists/VirtualizedList.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -1218,8 +1218,6 @@ class VirtualizedList extends React.PureComponent<Props, State> {
12181218
componentDidUpdate(prevProps: Props) {
12191219
const {data, extraData} = this.props;
12201220
if (data !== prevProps.data || extraData !== prevProps.extraData) {
1221-
this._hasDataChangedSinceEndReached = true;
1222-
12231221
// clear the viewableIndices cache to also trigger
12241222
// the onViewableItemsChanged callback with the new data
12251223
this._viewabilityTuples.forEach(tuple => {
@@ -1248,7 +1246,6 @@ class VirtualizedList extends React.PureComponent<Props, State> {
12481246
_fillRateHelper: FillRateHelper;
12491247
_frames = {};
12501248
_footerLength = 0;
1251-
_hasDataChangedSinceEndReached = true;
12521249
_hasDoneInitialScroll = false;
12531250
_hasInteracted = false;
12541251
_hasMore = false;
@@ -1546,20 +1543,22 @@ class VirtualizedList extends React.PureComponent<Props, State> {
15461543
} = this.props;
15471544
const {contentLength, visibleLength, offset} = this._scrollMetrics;
15481545
const distanceFromEnd = contentLength - visibleLength - offset;
1546+
const threshold = onEndReachedThreshold
1547+
? onEndReachedThreshold * visibleLength
1548+
: 0;
15491549
if (
15501550
onEndReached &&
15511551
this.state.last === getItemCount(data) - 1 &&
1552-
/* $FlowFixMe(>=0.63.0 site=react_native_fb) This comment suppresses an
1553-
* error found when Flow v0.63 was deployed. To see the error delete this
1554-
* comment and run Flow. */
1555-
distanceFromEnd < onEndReachedThreshold * visibleLength &&
1556-
(this._hasDataChangedSinceEndReached ||
1557-
this._scrollMetrics.contentLength !== this._sentEndForContentLength)
1552+
distanceFromEnd < threshold &&
1553+
this._scrollMetrics.contentLength !== this._sentEndForContentLength
15581554
) {
1559-
// Only call onEndReached once for a given dataset + content length.
1560-
this._hasDataChangedSinceEndReached = false;
1555+
// Only call onEndReached once for a given content length
15611556
this._sentEndForContentLength = this._scrollMetrics.contentLength;
15621557
onEndReached({distanceFromEnd});
1558+
} else if (distanceFromEnd > threshold) {
1559+
// If the user scrolls away from the end and back again cause
1560+
// an onEndReached to be triggered again
1561+
this._sentEndForContentLength = 0;
15631562
}
15641563
}
15651564

0 commit comments

Comments
 (0)