-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Fix onEndReached bugs for FlatList
#26444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
65d32fa
6600cbb
4374497
5451f8e
a4ab116
2d3f6ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1077,8 +1077,6 @@ class VirtualizedList extends React.PureComponent<Props, State> { | |
| componentDidUpdate(prevProps: Props) { | ||
| const {data, extraData} = this.props; | ||
| if (data !== prevProps.data || extraData !== prevProps.extraData) { | ||
| this._hasDataChangedSinceEndReached = true; | ||
|
|
||
| // clear the viewableIndices cache to also trigger | ||
| // the onViewableItemsChanged callback with the new data | ||
| this._viewabilityTuples.forEach(tuple => { | ||
|
|
@@ -1107,7 +1105,6 @@ class VirtualizedList extends React.PureComponent<Props, State> { | |
| _fillRateHelper: FillRateHelper; | ||
| _frames = {}; | ||
| _footerLength = 0; | ||
| _hasDataChangedSinceEndReached = true; | ||
| _hasDoneInitialScroll = false; | ||
| _hasInteracted = false; | ||
| _hasMore = false; | ||
|
|
@@ -1137,6 +1134,7 @@ class VirtualizedList extends React.PureComponent<Props, State> { | |
| _totalCellsMeasured = 0; | ||
| _updateCellsToRenderBatcher: Batchinator; | ||
| _viewabilityTuples: Array<ViewabilityHelperCallbackTuple> = []; | ||
| _hasDoneFirstScroll = false; | ||
|
|
||
| _captureScrollRef = ref => { | ||
| this._scrollRef = ref; | ||
|
|
@@ -1371,30 +1369,48 @@ class VirtualizedList extends React.PureComponent<Props, State> { | |
| return !this.props.horizontal ? metrics.y : metrics.x; | ||
| } | ||
|
|
||
| _maybeCallOnEndReached() { | ||
| const { | ||
| data, | ||
| getItemCount, | ||
| onEndReached, | ||
| onEndReachedThreshold, | ||
| } = this.props; | ||
| const {contentLength, visibleLength, offset} = this._scrollMetrics; | ||
| const distanceFromEnd = contentLength - visibleLength - offset; | ||
| _maybeCallOnEndReached(hasShrinkedContentLength: boolean = false) { | ||
| const {onEndReached, onEndReachedThreshold} = this.props; | ||
| if (!onEndReached) { | ||
| return; | ||
| } | ||
|
|
||
| const {contentLength, visibleLength, offset, dOffset} = this._scrollMetrics; | ||
|
|
||
| // If this is just after the initial rendering | ||
| if ( | ||
| onEndReached && | ||
| this.state.last === getItemCount(data) - 1 && | ||
| /* $FlowFixMe(>=0.63.0 site=react_native_fb) This comment suppresses an | ||
| * error found when Flow v0.63 was deployed. To see the error delete this | ||
| * comment and run Flow. */ | ||
| distanceFromEnd < onEndReachedThreshold * visibleLength && | ||
| (this._hasDataChangedSinceEndReached || | ||
| this._scrollMetrics.contentLength !== this._sentEndForContentLength) | ||
| !hasShrinkedContentLength && | ||
| !this._hasDoneFirstScroll && | ||
| offset === 0 | ||
| ) { | ||
| // Only call onEndReached once for a given dataset + content length. | ||
| this._hasDataChangedSinceEndReached = false; | ||
| this._sentEndForContentLength = this._scrollMetrics.contentLength; | ||
| onEndReached({distanceFromEnd}); | ||
| return; | ||
| } | ||
|
|
||
| // If scrolled up in the vertical list | ||
| if (dOffset < 0) { | ||
| return; | ||
| } | ||
|
|
||
| // If contentLength has not changed | ||
| if (contentLength === this._sentEndForContentLength) { | ||
| return; | ||
| } | ||
|
|
||
| const distanceFromEnd = contentLength - visibleLength - offset; | ||
|
|
||
| // If the distance is so farther than the area shown on the screen | ||
| if (distanceFromEnd >= visibleLength * 1.5) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. magic number 1.5
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TLDR LONG VERSION He includes same behaviour in the lines below. I believe his implementation accepts minimum |
||
| return; | ||
| } | ||
|
|
||
| // $FlowFixMe | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why here |
||
| const minimumDistanceFromEnd = onEndReachedThreshold * visibleLength; | ||
| if (distanceFromEnd >= minimumDistanceFromEnd) { | ||
| return; | ||
| } | ||
|
|
||
| this._sentEndForContentLength = contentLength; | ||
| onEndReached({distanceFromEnd}); | ||
| } | ||
|
|
||
| _onContentSizeChange = (width: number, height: number) => { | ||
|
|
@@ -1414,9 +1430,24 @@ class VirtualizedList extends React.PureComponent<Props, State> { | |
| if (this.props.onContentSizeChange) { | ||
| this.props.onContentSizeChange(width, height); | ||
| } | ||
| this._scrollMetrics.contentLength = this._selectLength({height, width}); | ||
| const {contentLength: currentContentLength} = this._scrollMetrics; | ||
| const contentLength = this._selectLength({height, width}); | ||
| this._scrollMetrics.contentLength = contentLength; | ||
| this._scheduleCellsToRenderUpdate(); | ||
| this._maybeCallOnEndReached(); | ||
|
|
||
| const hasShrinkedContentLength = | ||
| currentContentLength > 0 && | ||
| contentLength > 0 && | ||
| contentLength < currentContentLength; | ||
|
|
||
| if ( | ||
| hasShrinkedContentLength && | ||
| this._sentEndForContentLength >= contentLength | ||
| ) { | ||
| this._sentEndForContentLength = 0; | ||
| } | ||
|
|
||
| this._maybeCallOnEndReached(hasShrinkedContentLength); | ||
| }; | ||
|
|
||
| /* Translates metrics from a scroll event in a parent VirtualizedList into | ||
|
|
@@ -1503,6 +1534,7 @@ class VirtualizedList extends React.PureComponent<Props, State> { | |
| if (!this.props) { | ||
| return; | ||
| } | ||
| this._hasDoneFirstScroll = true; | ||
| this._maybeCallOnEndReached(); | ||
| if (velocity !== 0) { | ||
| this._fillRateHelper.activate(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.