I am trying to follow some tutorials on reactJS and I have a problem with understanding this example https://jsfiddle.net/reactjs/zafjbw1e/light/ . What is the purpose of bind(this) ( line 36) It seems that I can't pass state from parent to child without this bind method.
var ProductTable = React.createClass({
render: function() {
var rows = [];
var lastCategory = null;
this.props.products.forEach(function(product) {
if (product.name.indexOf(this.props.filterText) === -1 || (!product.stocked && this.props.inStockOnly)) {
return;
}
if (product.category !== lastCategory) {
rows.push(<ProductCategoryRow category={product.category} key={product.category} />);
}
rows.push(<ProductRow product={product} key={product.name} />);
lastCategory = product.category;
}.bind(this));
return (
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
);
}
});
I still don't understand this code, why we need bind(this) ? Why this.props.products doesn't require bind(this) and this.props.filterText require this ?