【发布时间】:2017-04-17 20:02:50
【问题描述】:
mapDispatchToProps 中有三个函数。我想在 React Component 的构造函数中使用它们的任何函数,但是当我使用 console.log(this.props)it 时,它给出了未定义的如何在构造函数中使用这些函数从 firebase 数据库加载数据?
mapDispatchToProps
const mapDispatchToProps = (dispatch) => {
return {
addProductRequest: (data) => {
console.log(data)
dispatch(AddNewProduct(data))
},
loadProducts: () => {
dispatch(ViewProducts())
},
loadStores: () => {
dispatch(ViewStores())
},
}
}
构造函数
constructor() {
super();
this.state = {
products: [],
stores: [],
currentProduct: [],
stockAvailable: [],
productName: '',
description: '',
qty: 0,
unitPrice: 0,
storeName: '',
selectedProduct: '',
productNameInStock: '',
productQtyInStock:0
}
console.log(this.props)
this.props.loadProducts();
this.props.loadStores();
this.submit = this.submit.bind(this);
this.inputHandler = this.inputHandler.bind(this);
}
报错
未捕获的类型错误:无法读取未定义的属性“loadProducts”
【问题讨论】:
-
而不是使用
mapDispatchToProps,为什么不这样做this.props.dispatch(ViewProducts())。在我看来要清楚得多。 -
我可以用但是我想用在mapDispatchToProps中,谢谢你的建议
标签: reactjs redux react-redux