I have to call API when the user stops typing and it is working perfectly fine. And I have to mount when the enter key is pressed. I made a Mock Component here which does this.
But, when the component is unmounted it shows the error Cannot call setState on an unmounted component
. Previously I handled this error with this.isMounted
. Now I was trying to handle it using promise cancelling in componentWillUnmount as mentioned in the React Blog.
this.cancellablePromise = makeCancelable(getSearchResults(word));
this.cancellablePromise.promise
.then(res => {
console.log({ res });
this.setState({ values: res });
})
.catch(err => console.log("error", err));
console.log("in data ", this.cancellablePromise);
}
The cancellablePromise gets assigned after the promise got resolved. So there is a null object in componentWillUnMount for cancellablePromise instance.