facebook/react — pull request #583 by spicyj (2013-11-21 08:07:09+00:00)
Title: RFC: Remove autobinding from ReactCompositeComponent

Autobinding causes unnecessary allocations and is counterintuitive because objects in JavaScript rarely have bound methods. 99% of the time, autobinding is useful only because it causes event handlers to be run with the correct context. Let's instead simply run event listeners in the context of the _owner_ component that added them, avoiding the need to autobind.

This new strategy of managing event callback contexts differs from the current event behavior in one practical way -- when callbacks are passed from a parent component down:

```js
var Feed = React.createClass({
render: function() {
return this.props.comments.map(function(comment) {
return ;
}, this);
},
onCommentDelete: function() { ... }
});
var Comment = React.createClass({
render: function() {
return

{this.props.comment.text}
Delete

;
}
});
```

This change causes onDelete to be run with `this` equal to the clicked Comment component rather than the Feed component. At a first glance, this might seem undesireable. However, note that this is in fact quite useful: Feed.onCommentDelete can read from `this.props.key` or `this.props.comment` to identify which comment was clicked. Instead of needing to do a bind in the render method, we can reuse the same function without making extra allocations.