reactjs – React-listen滚动向上/向下?

我正在构建一个应用程序(ES6),我很好奇什么是正确的方法来捕捉向上/向下滚动事件?

我试过(npm)安装react-scroll-listener但我无法使用我的ES6类.

我基本上想要:如果向上滚动,请执行此操作;如果向下滚动,请执行其他操作.

import React from 'react';
import config from '../config';
import StaticImageList from '../Common/StaticImageList';
import ScrollListener from 'react-scroll-listener';

class Album extends React.Component {
    constructor(props){
      super(props);

    }
    myScrollStartHandler(){
        console.log('Scroll start');

    }
    myScrollEndHandler(){
        console.log('Scroll end 300ms(default)');
    }
    componentDidMount(){
      scrollListener.addScrollHandler('body',myScrollStartHandler,myScrollEndHandler );
    }
    render(){
      return <StaticImageList />;
    }
};

export default Album;
这是挂钩任何听众的一般建议:

在componentDillMount中附加内容,在componentwillUnmount中取消附加

class Whatever extends Component {
  componentDidMount() {
    window.addEventListener('scroll',this.handleScroll,{ passive: true })
  }

  componentwillUnmount() {
    window.removeEventListener('scroll',this.handleScroll)
  }

  handleScroll(event) {
    // do something like call `this.setState`
    // access window.scrollY etc
  }
}

相关文章

react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...
react 本身提供了克隆组件的方法,但是平时开发中可能很少使...
mobx 是一个简单可扩展的状态管理库,中文官网链接。小编在接...
我们在平常的开发中不可避免的会有很多列表渲染逻辑,在 pc ...