From 29330d0e8657de1d0d2f94880e4eedcb0e117bdd Mon Sep 17 00:00:00 2001 From: Ramakrishna Date: Sun, 13 Oct 2019 22:02:04 +0530 Subject: [PATCH] Added support for async loadmore function Fixes https://github.com/CassetteRocks/react-infinite-scroller/issues/214 --- src/InfiniteScroll.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/InfiniteScroll.js b/src/InfiniteScroll.js index b08637e..a77614a 100644 --- a/src/InfiniteScroll.js +++ b/src/InfiniteScroll.js @@ -38,6 +38,9 @@ export default class InfiniteScroll extends Component { this.scrollListener = this.scrollListener.bind(this); this.eventListenerOptions = this.eventListenerOptions.bind(this); this.mousewheelListener = this.mousewheelListener.bind(this); + this.state = { + loading: false + }; } componentDidMount() { @@ -145,7 +148,7 @@ export default class InfiniteScroll extends Component { attachScrollListener() { const parentElement = this.getParentElement(this.scrollComponent); - if (!this.props.hasMore || !parentElement) { + if (this.state.loading || !this.props.hasMore || !parentElement) { return; } @@ -216,8 +219,11 @@ export default class InfiniteScroll extends Component { this.beforeScrollHeight = parentNode.scrollHeight; this.beforeScrollTop = parentNode.scrollTop; // Call loadMore after detachScrollListener to allow for non-async loadMore functions - if (typeof this.props.loadMore === 'function') { - this.props.loadMore((this.pageLoaded += 1)); + if (typeof this.props.loadMore === 'function' && !this.state.loading) { + this.setState({ loading: true }); + this.props.loadMore((this.pageLoaded += 1)).then(() => { + this.setState({ loading: false }); + }).catch(() => this.setState({ loading: false })); this.loadMore = true; } }