From b0bdd4e45d6de4a8f60cffe8d82f063381088f6c Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Mon, 5 Oct 2015 13:42:25 -0700 Subject: [PATCH] Fix ListView bug where onEndReached wouldn't trigger initially MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: @​public Sometimes we want to load a very small number of rows initially and want the onEndReached callback to be called immediately to trigger more data to be loaded without waiting for the user to scroll at all. This diff makes that happen by also checking on mount instead of only when scrolling. Reviewed By: @vjeux Differential Revision: D2507184 fb-gh-sync-id: ea8e47667d00387a935a426dd45afe978fd6d8cd --- .../CustomComponents/ListView/ListView.js | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/Libraries/CustomComponents/ListView/ListView.js b/Libraries/CustomComponents/ListView/ListView.js index 4554eb681..834de83a0 100644 --- a/Libraries/CustomComponents/ListView/ListView.js +++ b/Libraries/CustomComponents/ListView/ListView.js @@ -452,10 +452,23 @@ var ListView = React.createClass({ this._updateVisibleRows(childFrames); }, + _maybeCallOnEndReached: function(event) { + if (this.props.onEndReached && + this.scrollProperties.contentLength !== this._sentEndForContentLength && + this._getDistanceFromEnd(this.scrollProperties) < this.props.onEndReachedThreshold && + this.state.curRenderedRowsCount === this.props.dataSource.getRowCount()) { + this._sentEndForContentLength = this.scrollProperties.contentLength; + this.props.onEndReached(event); + return true; + } + return false; + }, + _renderMoreRowsIfNeeded: function() { if (this.scrollProperties.contentLength === null || this.scrollProperties.visibleLength === null || this.state.curRenderedRowsCount === this.props.dataSource.getRowCount()) { + this._maybeCallOnEndReached(); return; } @@ -570,14 +583,7 @@ var ListView = React.createClass({ isVertical ? 'y' : 'x' ]; this._updateVisibleRows(e.nativeEvent.updatedChildFrames); - var nearEnd = this._getDistanceFromEnd(this.scrollProperties) < this.props.onEndReachedThreshold; - if (nearEnd && - this.props.onEndReached && - this.scrollProperties.contentLength !== this._sentEndForContentLength && - this.state.curRenderedRowsCount === this.props.dataSource.getRowCount()) { - this._sentEndForContentLength = this.scrollProperties.contentLength; - this.props.onEndReached(e); - } else { + if (!this._maybeCallOnEndReached(e)) { this._renderMoreRowsIfNeeded(); }