I'm doing an infinite scroll in ReactJS but I'm in trouble!
After my component loads, I do this:
componentDidMount() {
window.addEventListener('scroll', function() {
var h = this.innerHeight;
var j = document.documentElement.scrollHeight;
var k = document.documentElement.scrollTop;
if ((h + k) >= j - 150) {
loadPhotos();
}
});
}
and, as a magic, I've "loadPhotos() is not defined". I can't use this.loadPhotos() because it refers to window (that hasn't loadPhotos()).
I initialize my loadPhotos() in the constructor() method:
this.loadPhotos = this.loadPhotos.bind(this);
My loadPhotos() is defined outside the constructor() method, I mean that is defined in the class body.
Someone can help me? Thank you guys!
SOLUTION
componentDidMount() {
window.addEventListener('scroll', () => { // arrow boys
var h = window.innerHeight;
var j = document.documentElement.scrollHeight;
var k = document.documentElement.scrollTop;
if ((h + k) >= j - 150) {
this.loadPhotos();
}
});
}