Muhammad Ateeq Azam Muhammad Ateeq Azam - 28 days ago 21
React JSX Question

Send props in .map() function

I have following props in my current component

static propTypes = {
oPurchaseDetails: PropTypes.array,
updateCart: PropTypes.func.isRequired,
}


Now I want to send to CartItem component. But It doesn't work there.

this.props.oPurchaseDetails.map(function(item) {
return (
<CartItem oProductItem={item} updateCart={this.props.updateCart} />
);
})


How can I send the prop function from .map() function?

Answer

map accepts a second parameter that sets your this context:

this.props.oPurchaseDetails.map(function(item) {
    return (
        <CartItem oProductItem={item} updateCart={this.props.updateCart} />
    );
}, this);