How to add scroll event in react component

前端 未结 6 1987
逝去的感伤
逝去的感伤 2021-01-03 20:39

I\'m trying to add an onScroll event on a table. This is what I\'ve tried:

componentDidMount() {
    ReactDOM.findDOMNode(this.refs.table).addEv         


        
相关标签:
6条回答
  • 2021-01-03 20:59

    Give this a try, added .bind(this) to this.listenScrollEvent when you pass it to the addEventListener.

       componentDidMount() {
            ReactDOM.findDOMNode(this.refs.table).addEventListener('scroll', this.listenScrollEvent.bind(this));
        }
    
        componentWillUnmount() {
            ReactDOM.findDOMNode(this.refs.table).removeEventListener('scroll', this.listenScrollEvent.bind(this));
        }
    
        listenScrollEvent() {
            console.log('Scroll event detected!');
        }
    
        render() {
            return (
                <table ref="table">
                   [...]
                </table>
            )
        }
    
    0 讨论(0)
  • 2021-01-03 21:05

    You can use onScroll attribute:

    listenScrollEvent() {
        console.log('Scroll event detected!');
    }
    
    render() {
        return (
            <table onScroll={this.listenScrollEvent}>
               [...]
            </table>
        )
    }
    

    Here is an example: https://jsfiddle.net/81Lujabv/

    0 讨论(0)
  • 2021-01-03 21:08

    You need to bind this to the element in context.

    render() {
        return (
            <table ref="table" onScroll={this.listenScrollEvent.bind(this)}>
               [...]
            </table>
        )
    }
    
    0 讨论(0)
  • 2021-01-03 21:17

    according to React documents(https://reactjs.org/docs/handling-events.html),

    React events are named using camelCase, rather than lowercase. You can set attributes as you do with pure HTML.

    HTML:

    <div onclick="..." onscroll="...">
      ...
    </div>
    

    JSX:

    <div onClick={...} onScroll={...}>
      ...
    </div>
    

    you should create a wrapper block element which has fixed height to enable scroll.

    0 讨论(0)
  • 2021-01-03 21:19

    I had been finding a good solution to this problem. The following piece of code is working, where the listener is just on the particular class/div : React version is 16.0.0

    First import ReactDOM, import ReactDOM from "react-dom";

    Then in the class xyz extends Component section

      constructor(props) {
        super();
        this.state = {
            vPos : 0
        }
        this.listenScrollEvent = this.listenScrollEvent.bind(this);
      }
    
      componentDidMount() {
        ReactDOM.findDOMNode(this.refs.category_scroll).addEventListener('scroll', this.listenScrollEvent);
      }
    
      componentWillUnmount() {
          ReactDOM.findDOMNode(this.refs.category_scroll).removeEventListener('scroll', this.listenScrollEvent);
      }
    
      listenScrollEvent(event) {
        console.log('firee');
        this.setState({
            vPos: event.target.body.scrollTop
        });
      }
    

    After pasting the above functions, in the render() method , whatever you want to name the ref method, you can do so, make sure the same name goes in the findDOMNode as well , i.e, findDOMNode(this.refs.category_scroll):

    <div onScroll={this.listenScrollEvent} ref="category_scroll">
    ...
    </div>
    

    .

    .

    If it's a horizontall scroll, then event.currentTarget.scrollTop works in the listenScrollEvent() function.

    0 讨论(0)
  • 2021-01-03 21:25

    I was looking to do something similar. Adding the event listener to the window instead of the ReactDom.findDOMNode worked for me...

    componentDidMount() {
        window.addEventListener('scroll', this.handleScrollToElement);
    }
    
    componentWillUnmount() {
        window.removeEventListener('scroll', this.handleScrollToElement);
    }
    
    handleScrollToElement(event) {
        console.log('Fired ' + event)
    }
    
    0 讨论(0)
提交回复
热议问题