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

[Code Style] automatic listener setup for componentWillMount & componentWillUnmount #1896

Closed
kostiakoval opened this issue Jul 7, 2015 · 7 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@kostiakoval
Copy link
Contributor

If I want to handle some events, I need to register and unregister manually now

 componentWillMount() {
    SomeComponent.addEventListener('event', this._event);
  }
  componentWillUnmount() {
     SomeComponent.removeEventListener('event', this._event);
  }
  _event() {
     ...
  }

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 :)

componentLifeBindings() {
   return [ {SomeComponent, 'event', this._event},
            {View, 'move', this._hadleMovement}]
}
@nicklockwood
Copy link
Contributor

You can use mixins to manage lifecycle stuff like this (this is how timers are handled). Maybe @ericvicenti can provide more info?

@brentvatne brentvatne changed the title automatic listener setup for componentWillMount & componentWillUnmount [Code Style] automatic listener setup for componentWillMount & componentWillUnmount Jul 7, 2015
@brentvatne
Copy link
Collaborator

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.

@christopherdro
Copy link
Contributor

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"

@brentvatne
Copy link
Collaborator

Nice one @christopherdro 😄

@kostiakoval
Copy link
Contributor Author

Thanks for answers! Great resources

@browniefed
Copy link
Contributor

@brentvatne still keep this open with all the solutions out there like Subscribable?

@brentvatne
Copy link
Collaborator

@browniefed - yeah I think those solutions are good, close seems appropriate!

@facebook facebook locked as resolved and limited conversation to collaborators Jul 22, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

6 participants