I'm doing an infinite scroll in ReactJS but I'm in trouble!
After my ponent loads, I do this:
ponentDidMount() {
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
ponentDidMount() {
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();
}
});
}
I'm doing an infinite scroll in ReactJS but I'm in trouble!
After my ponent loads, I do this:
ponentDidMount() {
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
ponentDidMount() {
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();
}
});
}
Share
Improve this question
edited Dec 21, 2018 at 23:45
Stackedo
asked Dec 21, 2018 at 23:37
StackedoStackedo
9332 gold badges9 silver badges22 bronze badges
2 Answers
Reset to default 7Use an arrow function as the callback, and this will refer to the ponent's instance.
Because the original this
inside the callback referred to window
, you also need to change this.innerHeight
to window.innerHeight
.
ponentDidMount() {
window.addEventListener('scroll', () => { // arrow function
var h = window.innerHeight;
var j = document.documentElement.scrollHeight;
var k = document.documentElement.scrollTop;
if ((h + k) >= j - 150) {
this.loadPhotos(); // now you can use this
}
});
}
If you want to use your way,
You would fix issue by using let _self = this
.
Like this
ponentDidMount() {
let _self = this;
window.addEventListener('scroll', function() {
var h = window.innerHeight;
var j = document.documentElement.scrollHeight;
var k = document.documentElement.scrollTop;
if ((h + k) >= j - 150) {
_self.loadPhotos();
}
});
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742306398a4419026.html
评论列表(0条)