我需要将props
传递给子组件Product
但我不知道我在这里错过了什么。不能将道具从父组件传递给子组件
父组件:
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>
)
}
});
子组件:
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)
});
}
});
我能使用key
和product
作为组件内的道具,但不this.props.handleDeleteProduct
和this.props.handleEditProduct
。 我想可能是我在success
回调$.ajax
内使用props
,然后可能会有一些东西与异步请求相关。不确定。
我得到的错误是
Uncaught TypeError: _this.props.handleEditProduct is not a function
我不知道我在做什么错在这里。我试图直接在<tbody>
之间循环,但仍然没有运气。 在这里我打电话的功能,如this.deleteProduct
作为参考,但不是通过函数调用。如果我通过函数调用,那么它报告execjs
错误。
我把这个作为参考的内部JSX
循环:loop inside React JSX
,但没有运气。请帮忙。
我不是看到'handleEditProduct'功能'Product' – bryce
另外,这行'formData = {this.props。}'的意义是什么?这是一个错字吗? – taylorc93
@guavacat:它的成功写入:'返回_this.props.handleEditProduct(_this.props.product,data);' –