I am trying to fetch data after I scroll down at the bottom of the element. This part is working that it fetches when I scroll, but the behavior is not correct. When it fetches, the data renders so many times and also not in right order. It looks fine before I scroll. It shouldn't do that. I am using MobX.
The problem must be in reaction somehow:
componentDidMount() {
const { listData } = this.props;
const { data = [] } = listData;
const rowData = [...data].reverse();
this.ref.current.addEventListener('scroll', this.handleScroll, false);
this.handleScroll();
reaction(
() => rowData,
() => this.handleScroll()
); }
And render looks like this:
render() {
const { listData } = this.props;
const { data = [] } = listData;
const rowData = [...data].reverse();
return (
<>
<div className="container" ref={this.ref}>
<ul className="list">
{_.map(rowData, (item, index) => (
<li
className="some-list"
key={index}
>
<div className="item-wrapper">
<div className="item">
<div className="item-name">
List {item.number}
</div>
</div>
</div>
</li>
))}
</ul>
</div>
</>
Code for handle scroll:
handleScroll() {
const { listData } = this.props;
const { paging = [] } = listData;
const contentHolder = this.ref.current;
const margin = 100;
const scrollY = contentHolder.scrollTop;
const wrapperHeight = contentHolder.offsetHeight;
const list = contentHolder.querySelector('.container');
if (wrapperHeight + scrollY >= list.offsetHeight - margin) {
this.handleRecieveFetch(paging.next);
}
}
The code is in the same component. Do you have any ideas what I am doing wrong?