From 506138f7b3dcbe88efdbfec267d400ecbbf1fd86 Mon Sep 17 00:00:00 2001 From: Dominick Lee Date: Mon, 22 May 2017 18:11:54 +0900 Subject: [PATCH] add isRestart to check for startover add componentWillReceiveProps to check props and detach when hasMore is false --- src/InfiniteScroll.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/InfiniteScroll.js b/src/InfiniteScroll.js index 1d6be43..4385133 100644 --- a/src/InfiniteScroll.js +++ b/src/InfiniteScroll.js @@ -19,6 +19,7 @@ export default class InfiniteScroll extends Component { PropTypes.array, ]).isRequired, loader: PropTypes.object, + isRestart: PropTypes.bool, }; static defaultProps = { @@ -31,27 +32,35 @@ export default class InfiniteScroll extends Component { isReverse: false, useCapture: false, loader: null, + isRestart: false, }; constructor(props) { super(props); - this.scrollListener = this.scrollListener.bind(this); } componentDidMount() { this.pageLoaded = this.props.pageStart; - this.attachScrollListener(); + if(this.props.hasMore) + this.attachScrollListener() } - componentDidUpdate() { - this.attachScrollListener(); + componentWillReceiveProps(nextProps) { + this.props = nextProps; + if(!this.props.hasMore) + this.detachScrollListener(); + if(this.props.isRestart) + this.pageLoaded = this.props.pageStart; } + componentDidUpdate() { + if(this.props.hasMore) + this.attachScrollListener(); + } componentWillUnmount() { this.detachScrollListener(); } - // Set a defaut loader for all your `InfiniteScroll` components setDefaultLoader(loader) { this.defaultLoader = loader; @@ -137,6 +146,7 @@ export default class InfiniteScroll extends Component { threshold, useCapture, useWindow, + isRestart, ...props } = this.props;