Respond onScroll Do Not Shoot

I have a simple reaction component, I set the onScroll event for this component, but when I scroll it, I don’t shoot

import React, { Component, PropTypes } from 'react'

export default class MyComponent extends Component {
  _handleScroll(e) {
    console.log('scrolling')
  }

  render() {
    const style = {
      width: '100px',
      height: '100px',
      overflowY: 'hidden'
    }
    const innerDiv = {
      height: '300px',
      width: '100px',
      background: '#efefef'
    }
    return (
      <div style={style} onScroll={this._handleScroll}>
        <div style={innerDiv}/>
      </div>
    )
  }
}
+4
source share
3 answers

You need to bind the _handleScroll event in the constructor. Try adding this to your component.

constructor() {
  this._handleScroll = this._handleScroll.bind(this);
}

https://facebook.imtqy.com/react/docs/handling-events.html

+2
source

You need to change the value overflowYto autoor scroll. You are not getting a scroll bar right now, because it hiddencauses the browser to hide the scroll bar.

+2
source

DOM:

onScroll

class ScrollingApp extends React.Component {

    _handleScroll(ev) {
        console.log("Scrolling!");
    }
    componentDidMount() {
        const list = ReactDOM.findDOMNode(this.refs.list)
        list.addEventListener('scroll', this._handleScroll);
    }
    componentWillUnmount() {
        const list = ReactDOM.findDOMNode(this.refs.list)
        list.removeEventListener('scroll', this._handleScroll);
    }
    /* .... */
}
+1

Source: https://habr.com/ru/post/1670081/


All Articles