Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the usage of isMounted() #2573

Closed
14 tasks done
oliviertassinari opened this issue Dec 17, 2015 · 9 comments · Fixed by #3437
Closed
14 tasks done

Remove the usage of isMounted() #2573

oliviertassinari opened this issue Dec 17, 2015 · 9 comments · Fixed by #3437
Labels
umbrella For grouping multiple issues to provide a holistic view

Comments

@alitaheri
Copy link
Member

👍 👍 👍

@oliviertassinari
Copy link
Member Author

@subjectix I have started doing it on my PR updating the Snackbar.

@alitaheri
Copy link
Member

@oliviertassinari very nice 👍

@Illyism
Copy link

Illyism commented Dec 17, 2015

You seem to be having trouble with this sometimes because you're adding and removing events that don't refer to the same function such as in mixins/click-awayable.js. bind() returns a new function

Something like this would solve this:

this.func = this._checkClickAway.bind(this);
Events.on(document, 'mouseup', this.func);
Events.off(document, 'mouseup', this.func);;

@newoga
Copy link
Contributor

newoga commented Feb 17, 2016

@oliviertassinari @alitaheri Are we following a general strategy for removing this?

I know the anti-pattern blog post mentioned an easy workaround would be to set a property on the class like this._isMounted to true and false during mounting and unmounting. Is that enough? After looking at the code for the remaining components, our use of it seems mostly related to animations and timeouts. I don't know if there's a cleaner way to remove this...

@alitaheri
Copy link
Member

@newoga there is. It's cancellation take a look at linear-progress. The better pattern is to have instance fields hold the handles. In other words the lifecycle is like the following:

  1. assign the value returned by setTimeout to the instance field ( anywhere in the code that it's used)
  2. for each unique concurrently executable timer there should be one field (linear-progress needs 2)
  3. with each call to componentWillUnmount clear every timeout.

Simple, efficient and pattern friendly 👍

@newoga
Copy link
Contributor

newoga commented Feb 17, 2016

@alitaheri That makes sense, thanks!

@oliviertassinari
Copy link
Member Author

I have updated the list of components that need to be fixed.
At least half of them is using isMounted() in a timeout, clearTimeout will do the job.
The others, may need a this._isMounted check.

@newoga
Copy link
Contributor

newoga commented Feb 18, 2016

Thanks @oliviertassinari!

I'm hoping to start tackling this after we finish the removal click-awayable.js 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
umbrella For grouping multiple issues to provide a holistic view
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants