constructor(props) {
super(props);
this.state = {
...
loadingMore: false,
};
...
this.onScroll = this.onScroll.bind(this);
}
componentDidMount() {
window.addEventListener('scroll', this.onScroll, true);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.onScroll);
}
onScroll() {
let scroll = $('#scroll-to');
let hT = scroll.offset().top;
let wH = $(window).height();
//
if (((hT - wH) <= 0) && !this.state.loadingMore) {
this.setState({loadingMore: true});
this.METHOD_WITH_YOUR_LOGIC();
}
};
METHOD_WITH_YOUR_LOGIC() {
// your logic here
this.setState({loadingMore: false})
}
render() {
return (
<div>
...
<div id='scroll-to'>
</div>
</div>
)
}
!!! important !!!
- you need add to end of your return component
- if you have issue with $ you should https://stackoverflow.com/questions/33351288/jquery-in-react-is-not-defined
- you must add this.setState({loadingMore: false}) in your METHOD_WITH_YOUR_LOGIC function