In my react app i am passing down my function reference to the child component using props onIncQty={this.handleIncreaseQty }
. The function is declared as:
handleIncreaseQty = (product) => {
const { products } = this.state;
const index = products.indexOf(product);
products[index].qty += 1
this.setState({
products : products
//we could have written simple products too
})
}
Now in the child component there is an onClick event which calls the parent method. It is called as
onClick={() => onIncQty(product)}
Now why are we using callback here when we are already binding the handleIncreaseQty
function when declaring it. We could have simply written
onClick={onIncQty(product)}
but it does not work why is that? Do we need to bind it again? Because the following code also works:
onClick={onIncQty.bind(this,product)}