Wimal Weerawansa
Wimal Weerawansa

Reputation: 157

React onScroll not firing

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>
    )
  }
}

Upvotes: 22

Views: 45559

Answers (2)

nathan
nathan

Reputation: 4742

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.

Upvotes: 23

Conan
Conan

Reputation: 690

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);
    }
    /* .... */
}

Upvotes: 1

Related Questions