react-google-maps-api icon indicating copy to clipboard operation
react-google-maps-api copied to clipboard

How to handle componentWillUnmount memory leakage?

Open hellocaio opened this issue 4 years ago • 0 comments

I'm having a problem with this library when the user navigates away from the page before LoadScript (asynchronous task) finishes loading the scripts. I get a warning stating memory leakage in my app.

How can I cancel this asynchronous task in this case?

What I usually do is something like this:

constructor(props) {
  this._isMounted = false;
}

componentWillMount() {
  this._isMounted = true;
}

componentWillUnmount() {
  this._isMounted = false
} 

doAsyncTask() {
  fetch("/data").then(res => {
    if (!this._isMounted) return;
    
    // Do something
  })
}

But in this case I am not sure how to cancel LoadScript, is there a prop I can return false to prevent it from loading to an unmounted component?

Thanks.

hellocaio avatar Jan 27 '21 14:01 hellocaio