Load more when scrolled to bottom in Reactjs

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Load more when scrolled to bottom in Reactjs



This is my post-card page(only the specific part) where i am displaying data from fetched Api.


fetchMoreData(){
this.setState({
index: this.state.index + 5
})
}
componentDidMount(){
window.addEventListener('scroll', this.onScroll);
this.fetchMoreData();
}

onScroll = () => {
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
this.fetchMoreData();
}
});
}



As My API is a testing API i fetch all the data in my posts page and then pass it to my post-card page so on change of this.state.index it displays more item.



The issue I am facing is that I get an error that this.fetchMoreData() is not a function. Feel free to point out any mistakes.




2 Answers
2



Make fetchMoreData an arrow function just like onScroll


fetchMoreData


onScroll


fetchMoreData = () => {
...
}





Still gives me the same error.
– Irtaza Rawjani
2 mins ago





See @gor Alemasow answer. That will work.
– Abdullah
33 secs ago


onScroll = () => {
$(window).scroll(() => {
if ($(window).scrollTop() + $(window).height() == $(document).height()) {
this.fetchMoreData();
}
});
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived