【问题标题】:Cannot read property 'scrollRequest' of undefined even though it should be无法读取未定义的属性“scrollRequest”,即使它应该是
【发布时间】:2020-03-15 00:52:21
【问题描述】:

我正在尝试构建某种平滑滚动 - 无论如何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));
   }
}

【问题讨论】:

    标签: reactjs typescript


    【解决方案1】:

    this 无法访问该值。像这样使用箭头函数:

    componentDidMount = () => {
        // ...
    }
    
    onScroll = () => {
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-22
      • 2021-03-11
      • 2020-05-31
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 2017-11-10
      • 2021-11-18
      相关资源
      最近更新 更多