window.removeEventListener not working on componentWillUnmount

Viewed 77

This is my code, using in component, but when i route to another URL the console gives me error -

ERROR

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.


  componentDidMount = async () => {
   // await myAPI call
   window.addEventListener("scroll", this.onWindowscroll);
  }

  onWindowscroll = async () => {
    console.log('onWindowscroll);
    // await myAPI call when scroll reach to bottom.
  }

  componentWillUnmount = () => {
    window.removeEventListener('scroll', this.onWindowscroll);
    console.log('componentWillUnmount H');
  }


Don't know why this is coming! This ERROR comes when I hard refresh and go to any other route.

As I am using arrow functions so I don't need .bind to be used in mounting time.

0 Answers
Related