element.scrollTop - how many scrolled from top,
element.scrollHeight - element's whole scroll height (with content visible and invisible),
element.offsetHeight - current visible height
When scrolling down, scrollTop increases,
So you want to scroll down to the bottom,
in other words you want distance(height) moved from top plus height of element's visible part to be equal to whole element's height:
element.scrollTop + element.offsetHeight = element.scrollHeight
we can write this as:
element.scrollTop = element.scrollHeight - element.offsetHeight
So if you want some action happen before reaching the bottom, just substract this amount of pixels from element.scrollHeight, for example for 200px:
element.scrollTop = (element.scrollHeight - 200) - element.offsetHeight
or like this to make it work even after we pass the distance:
element.scrollTop >= element.scrollHeight - element.offsetHeight - 200
Aditionally, as using scroll inside element (div) with ref, you probably want to add event listener to scrollRef.current, not window
componentDidMount() {
this.loadList();
this.scrollRef.current.addEventListener("scroll", this.handleScroll, true);
}
and remove event listener on unmount
componentWillUnmount() {
this.scrollRef.current.removeEventListener("scroll", this.handleScroll);
}
This should work:
CodeSandbox demo