jamesmacaulay / react-bacon

A little module for using React with Bacon.js
MIT License
118 stars 7 forks source link

Add `onValue()` method for binding event handlers #7

Closed fson closed 8 years ago

fson commented 10 years ago

In our use of React and Bacon it's been a common use case to add handlers to some external stream (e.g. a Bacon.Bus passed to the component in props). These handlers must be unsubscribed when the component unmounts, which would typically result in tedious code like this:

componentWillMount: function () {
  this._fooUnsubscribe = this.props.bus.filter(/*...*/).onValue(this.foo);
  this._barUnsubscribe = this.props.bus.map(/*...*/).onValue(this.bar);
  this._bazUnsubscribe = stream.onValue(this.baz);
},
componentWillUnmount: function () {
  this._fooUnsubscribe();
  this._barUnsubscribe();
  this._bazUnsubscribe();
}

This pull request adds method onValue() to BaconMixin that can be used to add handlers that will automatically be unsubscribed when the component unmounts, allowing the previous example to be rewritten as:

componentWillMount: function () {
  this.onValue(this.props.bus.filter(/*...*/), this.foo);
  this.onValue(this.props.bus.map(/*...*/), this.bar);
  this.onValue(stream, this.baz);
}
fson commented 8 years ago

Closing because the project is no longer being maintained and I'm also not using BaconMixin anymore.