最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReactES6 onScroll event not getting triggered - Stack Overflow

programmeradmin1浏览0评论

I have an onScroll event in my render function. When the site is scrolled it should trigger the listenScrollEvent function which outputs some data. For some reason it is not not getting triggered. I have binded the function in constructor, but apparently it haven't help.

Working example

import React, {Component} from 'react';
import { render } from 'react-dom';
import Hello from './Hello';

export class Home extends Component {
    constructor(props) {
      super();
          this.listenScrollEvent = this.listenScrollEvent.bind(this);

  }
    listenScrollEvent() {
    console.log('Scroll event detected!');
  }
    render() {
    return (
      <div className="App"  onScroll={this.listenScrollEvent}>

What is Lorem Ipsum?aa
Lorem Ipsum is simply dummy text of the printing and taypesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).


Where does it e from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum es from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", es from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, acpanied by English versions from the 1914 translation by H. Rackham.

Where can I get some?
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, bined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.

      </div>
    );

  }
}

render(<Home />, document.getElementById('root'));

Thanks in advance!

I have an onScroll event in my render function. When the site is scrolled it should trigger the listenScrollEvent function which outputs some data. For some reason it is not not getting triggered. I have binded the function in constructor, but apparently it haven't help.

Working example

import React, {Component} from 'react';
import { render } from 'react-dom';
import Hello from './Hello';

export class Home extends Component {
    constructor(props) {
      super();
          this.listenScrollEvent = this.listenScrollEvent.bind(this);

  }
    listenScrollEvent() {
    console.log('Scroll event detected!');
  }
    render() {
    return (
      <div className="App"  onScroll={this.listenScrollEvent}>

What is Lorem Ipsum?aa
Lorem Ipsum is simply dummy text of the printing and taypesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).


Where does it e from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum es from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", es from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, acpanied by English versions from the 1914 translation by H. Rackham.

Where can I get some?
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, bined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.

      </div>
    );

  }
}

render(<Home />, document.getElementById('root'));

Thanks in advance!

Share Improve this question edited Jul 8, 2017 at 18:10 Alireza 105k27 gold badges277 silver badges173 bronze badges asked Jul 8, 2017 at 18:05 GiedriusGiedrius 6131 gold badge6 silver badges28 bronze badges 3
  • 1 Is the div the element that scrolls, or is the whole page scrolling? What you have would only detect scroll if the div has overflow: scroll and has more content than will fit in it. – loganfsmyth Commented Jul 8, 2017 at 18:10
  • I would take a look at this question – frithjof Commented Jul 8, 2017 at 18:11
  • I have made a working example which you can look at here : codesandbox.io/s/v22xP1J6m – Giedrius Commented Jul 8, 2017 at 18:12
Add a ment  | 

2 Answers 2

Reset to default 5

Fixed my problem by adding:

  ponentDidMount() {
    window.addEventListener('scroll', this.listenScrollEvent);
  }
  ponentWillUnmount() {
    window.removeEventListener('scroll', this.listenScrollEvent);
  }

This worked for me,

ponentDidMount() {
  window.addEventListener('scroll', this.listenScrollEvent,true);
}

references: https://github./facebook/react/issues/5042#issuement-359478860

发布评论

评论列表(0)

  1. 暂无评论