Remove Event Listener On Unmount React

后端 未结 4 1514
眼角桃花
眼角桃花 2020-12-02 22:16

I had higher order component in react like this:

export default function (InnerComponent) {
    class InfiniteScrolling extends React.Component {

        co         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 22:53

          componentDidMount() {
                window.addEventListener('scroll', this.onScroll, false);
            }
    
            componentWillUnmount() {
                window.removeEventListener('scroll', this.onScroll, false);
            }
            // use arrow function instead
            onScroll = () => { 
                if ((window.innerHeight + window.scrollY) >= (document.body.offsetHeight - 50)) {
                    const { scrollFunc } = this.props;
                    scrollFunc();
                }
            }
    

    or you can use Arrow functions , to solve .bind(this) problems it worked form just fine.

提交回复
热议问题