My plan is to load data when you scroll and reach the bottom of the page. I am using JQuery scroll to make it possible and it works fine if no data to display (just console log the data). But if I start displaying data, the scroll fire twice or something else firing twice which I do not know what. Here is my code:
import React from 'react';
class UsersBrowse extends React.Component {
render() {
let x = this.props;
$(window).unbind('scroll').scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
const {loadMore, currentPage} = x;
loadMore(currentPage);
}
});
const {scanUserResult, loading} = this.props;
console.log(this.props);
return (
<div>
{scanUserResult ? scanUserResult.map(users => (
<div key={users._id}>
<div>{users._id}</div>
</div>
)):<img src={loading} height="100" width="100" />}
</div>
);
}
}
export default UsersBrowse;
The code works fine if I just console log the data but when I display it on the page it skips like:
loads page 1 skip page 2 loads page 3 skip page 4 loads page 5
My expectation is, it should load:
page1 page2 page3 page4
It works if I do it like this:
import React from 'react';
class UsersBrowse extends React.Component {
render() {
const {scanUserResult, loading} = this.props;
let x = this.props;
$(window).unbind('scroll').scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
const {loadMore} = x;
loadMore();
}
});
console.log(this.props);
return (
<div>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
test<br/>
</div>
// <div>
// {scanUserResult ? scanUserResult.map(users => (
// <div key={users._id}>
// <div>{users._id}</div>
// </div>
// )):<img src={loading} height="100" width="100" />}
// </div>
);
}
}
export default UsersBrowse;
Any idea? Thank you