See here: https://reactjs.org/blog/2015/12/16/ismounted-antipattern.html And also here: How to cancel a fetch on componentWillUnmount And here: ismounted antipattern, track own property
In both cases they mention 3 approaches:
- In your
promise.resolve
checkthis.IsMounted()
, which React will return correctly for you, if the `Compounted Has Unmounted - In your
promise.resolve
check_isMounted
, which you have manually tracked inComponentWillUnmount()
method. - Use cancellable promises, so that you
promise
doesn't ever resolve. And this will solve all your problems and make it lovely.
Except, in the 3rd case your promise
will error()
, but might also error()
in other cases (e.g. the API is down).
So in reality the 3rd option boils down to:
- In your promise.error
check errorPayload.IsCancelled
, which you have manually tracked in the cancellablePromise
object, which has in turn been triggered by a manual call in ComponentWillUnmount
.
So all three are pretty much identical:
When you are handling your
promise
outcomes, check the value of this variable that is directly tied to whether the component has alreadyunmounted
.
Why do they assert that the 3rd option is better than the other 2, and that the 1st option is an antipattern.