当页面位于最底部时,我在尝试运行函数时遇到问题。当页面位于最底部时,我试图从数据库中获取更多数据。
我的代码结构看起来像这样
componentDidMount(){
this.getFeed();
window.addEventListener('scroll', function() {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
console.log("you're at the bottom of the page");
this.setState({render_count: this.state.render_count+1});
this.getFeed();
}
});
}
getFeed(){//get node.js data}
似乎this.state为null或某些原因。我收到一个错误“无法读取未定义的属性'render_count'”它也不读取this.getFeed要么()。
感谢所有帮助!
[function() {}
函数不会捕获您需要的this
。
改为使用箭头功能:() => { ... }
。
请参见https://stackoverflow.com/a/34361380/51685以获取更多详细信息。
Ps。记得也要在某个时候删除该事件侦听器。