Remove Event Listener On Unmount React
<p>I had higher order component in react like this:</p>

<pre><code>export default function (InnerComponent) {
    class InfiniteScrolling extends React.Component {

        constructor(props){
            super(props);
        }

        componentDidMount() {
            window.addEventListener('scroll', this.onScroll.bind(this), false);
        }

        componentWillUnmount() {
            window.removeEventListener('scroll', this.onScroll.bind(this), false);
        }

        onScroll() {
            if ((window.innerHeight + window.scrollY) &gt;= (document.body.offsetHeight - 50)) {
                const { scrollFunc } = this.props;
                scrollFunc();
            }
        }

        render() {
            return &lt;InnerComponent {...this.props} /&gt;;
        }
    }

    InfiniteScrolling.propTypes = {
        scrollFunc: PropTypes.func.isRequired
    };

    return InfiniteScrolling;
}
</code></pre>

<p>After unmounting the component which are been wrapped via <code>InfiniteScrolling</code>, they where still throwing the error like (when I did scrolling):</p>

<blockquote>
  <p>Warning: setState(...): Can only update a mounted or mounting
  component. This usually means you called setState() on an unmounted
  component. This is a no-op. Please check the code for the undefined
  component.</p>
</blockquote>

<p>Even though I did remove the <code>scroll</code> event on my component unmount. It didn't work. </p>

<p>But when I changed the code to like this:</p>

<pre><code>constructor(props){
    super(props);
    this.onScroll = this.onScroll.bind(this);
}

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

componentWillUnmount() {
    window.removeEventListener('scroll', this.onScroll, false);
}
</code></pre>

<p>everything seems to be working fine, without any issues. </p>

<p>I feel they are exactly the same thing, but the second one works fine while the first one was throwing up the error in the console as mentioned before!</p>