即使应读取未定义的属性“ scrollRequest”,也无法读取

时间:2020-03-15 00:52:21

标签: reactjs typescript

我正在尝试构建某种平滑的滚动-无论如何,this.scroller.scrollRequest是在类中预定义的静态值:

scroller = {
   target: null,
   ease: 0.05,
   endY: 0,
   y: 0,
   resizeRequest: 1,
   scrollRequest: 0,
};

此值在滚动时被更改,似乎在滚动scrollRequest上未定义。怎么这样?

无法读取未定义的属性'scrollRequest'


那就是它的名字:

componentDidMount() {
   this.fetchProjects();

   this.scroller.target = document.querySelector(".ch--scrollable-container");
   window.focus();

   window.addEventListener("resize", this.onResize());
   document.addEventListener("scroll", this.onScroll);
}

onScroll() {
   this.scroller.scrollRequest++; <-- causing the error
   if (!this.requestId) {
       this.requestId = requestAnimationFrame(this.updateScroller.bind(this));
   }
}

1 个答案:

答案 0 :(得分:1)

testList无法访问该值。使用如下箭头功能:

this