I need to pass props
to a child component Product
but i don't know what i'm missing here.
Parent Component:
var Products = React.createClass({
getInitialState: function(){..},
deleteProduct: function(){..},
updateProduct: function(){..},
render: function(){
var products = _.map(this.state.products, function(product){
return(
<Product key={product.id} product={product} handleDeleteProduct={this.deleteProduct} handleEditProduct={this.editProduct} formData={this.props.formData}/>
)
});
return (
<table>
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Price</th>
<th colSpan="4">Actions</th>
</tr>
</thead>
<tbody>
{products}
</tbody>
</table>
)
}
});
Child Component:
var Product = React.createClass({
console.log(this); //props: handleDeleteProduct: undefined, handleEditProduct: undefined
handleEdit: function() {
e.preventDefault();
data = {..};
$.ajax({
..,
success: (function(_this) {
console.log(_this); //props: handleDeleteProduct: undefined, handleEditProduct: undefined
return function(data) {
_this.setState({
edit: false
});
return _this.props.handleEditProduct(_this.props.product, data);
};
})(this)
});
}
});
I'm able to use key
and product
as a props inside the component but not this.props.handleDeleteProduct
and this.props.handleEditProduct
.
I think may be i'm using the props
inside the success
callback of the $.ajax
and then may be some thing related to async request. Not sure.
The error i'm getting is
Uncaught TypeError: _this.props.handleEditProduct is not a function
I'm not sure what i'm doing wrong here. I tried to loop directly in between <tbody>
but still no luck.
Also here i'm calling the functions like this.deleteProduct
as a reference but not by function call. And if i do by calling by function then it is reporting execjs
error.
I took this as a reference for looping inside JSX
: loop inside React JSX
But no luck. Please help.