为什么我们在 onClick 事件上使用 Bind 方法

Why we use Bind method on onClick event

我正在学习React.js。在教程中,作者将onClick 与bind 一起使用,但在某些地方他没有将bind 与onClick 一起使用。 我无法区分两者。

 <button onClick={this.handleAdd}>Add Item</button>

您可以使用 bind 将特定参数传递给处理程序方法。

例如:

render: function() {
    return _.map(list, function(item) {
        return <li onClick={this.clickItem.bind(this, item)}>{item.name}</li>;
    });
},
clickItem: function(item, event) {
    //do something with the clicked item
}

如果不需要注入参数,则不需要 bind 因为 React 总是在组件范围内调用处理程序方法 - although this is changing soon