React 监听浏览器onscroll事件

#22

这两个 this 指向的是同一个对象,React 组件的实例方法有个毛病,无法自动绑定到当前上下文,可以通过 this.fn.bind(this) 显式绑定,也可以通过箭头函数绑定。详情见链接 https://reactjs.org/docs/faq-functions.html#how-do-i-bind-a-function-to-a-component-instance

#23
 componentDidMount(){
      window.addEventListener('scroll',this.handleScroll,true)
}

componentWillUnmount(){
      window.removeEventListener('scroll',this.handleScroll,true)
}
#24

不知道怎么会扯到this上面去,this再影响也是影响this.handleScroll这个函数内部的this指向,跟传参数那个this没关系。多看看MDN文档,addEventListener这个函数的参数怎么传的。这个问题答案就是这个函数最后加一个true参数