21

I have simple react component, I set onScroll event to that component but when I scroll it's not firing

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>
    )
  }
}
Wimal Weerawansa
  • 309
  • 2
  • 15
  • 33

2 Answers2

20

You need to change the value of overflowY to auto or scroll. Right now you're not getting a scrollbar because hidden causes the browser to hide the scrollbar.

nathan
  • 4,612
  • 6
  • 32
  • 33
1

you need to add a ref to the DOM element:

React onScroll not working

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);
    }
    /* .... */
}
Community
  • 1
  • 1
Conan
  • 670
  • 7
  • 12