U
U
uzi_no_uzi2019-07-20 12:51:36
JavaScript
uzi_no_uzi, 2019-07-20 12:51:36

Why is the scroll event not firing?

There is this component:

class App extends React.Component {

  constructor() {
    super();

    window.onscroll = function() {
      console.log(1);
    }

  }

  componentDidMount() {
    window.onscroll = function() {
      console.log(1);
    }
  }



  render() {
    window.onscroll = function() {
      console.log(1);
    }
  }
}

Why isn't the scroll event fired anywhere?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2019-07-20
@uzi_no_uzi

Apparently, because the page does not have the ability to scroll. All content is placed in the height of the window.
If so, then for such cases use the mousewheel event. For cross-browser handling of this event, if the same scroll speed in different browsers is important, you need to use an adapter, like normalize-wheel

R
Roman Alexandrovich, 2019-07-20
@RomReed

try like this

componentDidMount() {
    window.addEventListener('scroll', this.handleScroll);
  }

  componentWillUnmount() {
    window.removeEventListener('scroll', this.handleScroll);
  }
   
  
  handleScroll = () => {
    alert("scroll");
  };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question