Closed
Description
isMounted
is already unavailable on ES6 classes, and we already have a warning saying we "might" remove them, but we don't actually have a github issue to deprecate them. As per our discussions today, we are basically agreed that we're going to start moving away from isMounted
and deprecate it. We still need to figure out some good stories around promises (and related use cases).
This issue is to track progress toward that goal.
For background, please read:
Activity
isMounted
dfilatov/vidom#162quantizor commentedon Nov 16, 2015
I don't agree with this. ES6 promises in particular cannot be reliably cancelled on
componentWillUnmount
, so removing the only way to check if the component is mounted before setState or another action is opening the way for a lot of hard to trace async bugs.jimfb commentedon Nov 16, 2015
@yaycmyk Thus the line:
Please read the background issues I listed, in particular: #2787 (comment)
quantizor commentedon Nov 16, 2015
I did read the comments. I just find the issues intractable.
nvartolomei commentedon Nov 16, 2015
Why promises cant be reliable cancelled? Any sources/proofs/examples?
On Monday, November 16, 2015, Evan Jacobs notifications@github.com wrote:
quantizor commentedon Nov 16, 2015
@nvartolomei Look at the ES6 promise spec.
zpao commentedon Nov 16, 2015
This is a longer term goal, not something that is happening immediately. But we want to track the planning and discussions in a single place and not across comments in every issue when this comes up. We are aware of the problem of Promises currently being uncancellable which is a major reason we haven't already done this.
jimfb commentedon Nov 16, 2015
@yaycmyk To over-simplify a very complex issue... the comments are saying... using
isMounted
to avoidsetState
for unmounted components doesn't actually solve the problem that thesetState
warning was trying to indicate - in fact, it just hides the problem. Also, callingsetState
as the result of a promise is a bit of an anti-pattern anyway, since it can cause race conditions which won't necessarily show up in testing. Thus we want to get rid of it, and figure out a "best practice" recommendation for using promises with React.I agree the issues are a bit inscrutable, but that's largely because it's a complex issue that we're still figuring out and don't yet have a canned response for.
quantizor commentedon Nov 16, 2015
We can agree to disagree on that one. There are times when content is being fetched asynchronously and you don't want to have to go through a full-scale rerender to pop that content in once it is resolved. I use it specifically in an infinite table view implementation where a full virtual rerender would be unnecessary.
jimbolla commentedon Nov 16, 2015
You might not be able to cancel a promise, but you can make it dereference the component on unmount, like so:
The important distinction is when
this.protect.unmount()
is called incomponentWillUnmount
, all callbacks get dereferenced, meaning the component gets dereferenced, and then when the promise completes, it just calls a no-op. This should prevent any memory leaks related to promises references unmounted components. source for protectFromUnmountthis.isMounted
in components jsx-eslint/eslint-plugin-react#37istarkov commentedon Nov 18, 2015
This simple method can be used to add cancel to any promise
EDIT: Updated for correctness/completeness.
HOW TO USE
quantizor commentedon Nov 18, 2015
Listing ways to sup-up ES6 promises to make them cancellable is besides the point. The intent should be to provide a solution that works WITH the spec rather than trying to work AROUND the spec.
ir-fuel commentedon Nov 20, 2015
I agree. Instead of simply checking if the component is still mounted when we receive the promise result we have to resort to all kinds of magic so we can "unbind" our promise from the component it's supposed to set its result in, clearly fighting against the way promises are designed.
To me it feels like overengineering a solution where a simple test is the easiest way to take care of this.
66 remaining items