-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
[Code Style] automatic listener setup for componentWillMount & componentWillUnmount #1896
Comments
You can use mixins to manage lifecycle stuff like this (this is how timers are handled). Maybe @ericvicenti can provide more info? |
Mixins are good for this, see react-timer-mixin for example. Alternatively, you could use some kind of compositional technique like ES7 decorators or a component enhancing functions. For example, check out @johanneslumpe's blog post about adding a gesture decorator. |
This might help. https://github.com/facebook/react-native/blob/8b93b99d4a769f86fffc95d65434f2f6a3fd827d/Libraries/Components/Subscribable.js According to @ericvicenti from a previous issue... "Subscribable.Mixin is something we use internally to make sure nobody forgets to unsubscribe to an emitter from a component on unmount. The mixin will automatically remove subscriptions added with this.addListenerOn" |
Nice one @christopherdro 😄 |
Thanks for answers! Great resources |
@brentvatne still keep this open with all the solutions out there like Subscribable? |
@browniefed - yeah I think those solutions are good, close seems appropriate! |
If I want to handle some events, I need to register and unregister manually now
Very often we forgot to unregister from events listening (a notification on iOS) and it could lead to dangerous result.
The component could automatically unregister from events when its become inactive (gets unmounted)
Swift implementation - https://gist.github.com/jverkoey/dfa473635a810ab436d3
Some ideas for implementation, not perfect :)
The text was updated successfully, but these errors were encountered: