Why do we use the Bind method for the onClick event

I am learning React.js. In the tutorial, the author uses onClick with the link, but in some places he does not use the onClick binding. I can not get the difference between the two.

 <button onClick={this.handleAdd}>Add Item</button>
+4
source share
1 answer

You can use bindto pass a specific argument to the handler method.

For instance:

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
}

If you don’t need to enter an argument, you don’t need to bind, since the reaction always calls the handler method in the component area - although this will change soon

+5
source

All Articles