什么可能导致我的代码中出现此错误?
componentDidMount(){
let container = document.getElementById('body');
container.onscroll = function () {
let height = this.clientHeight;
let scrollHeight = this.scrollHeight;
let scrollTop = this.scrollTop;
if (height + scrollTop === scrollHeight) {
this.setState({ display: 'loader'});
}
}
}
考虑到容器是一个 HTMLElement,它在任何地方都没有方法
setState
,所以我想你的代码的想法是在包含它的类上调用该方法,componentDidMount
或者更确切地说,在包含它的类上调用该方法。您可以尝试声明
this
对函数上下文之外的 引用onscroll
。