Recently I came across the issue of fetch()
and setState()
return to an unmounted component, resulting in an error.
I am looking for a simple solution that does not involve redux or cancelling fetch().
A partial solution might be to use a local variable this._isMounted
that is set to true at componentDidMount
and false at componentWillUnMount
Now, if you perform:
if (this._isMounted) setState({whatever})
This is almost complete. The hole is the period of time, after the condition check and until setState() completes. Is there a way, by means of semaphores to handle this issue.
For example, delay componentWillUnmount
access to this._isMounted
, by waiting for setState()
semaphore?
Delay may sound bad and not be appropriate in some cases, but semaphores and legacy operating systems solution do seem relevant where async operations take place IMO.