mapDispatchToProps中有三个函数.我想在React Component的构造函数中使用它们中的任何函数,但是当我使用console.log(this.props)时,它给出了undefined如何在构造函数中使用这些函数从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);
}
它给出了一个错误
您将使用来自redux的bindActionCreators,f.e.:Uncaught TypeError: Cannot read property ‘loadProducts’ of undefined
const mapDispatchToProps = (dispatch) => {
return {
...bindActionCreators({ loadProducts, loadStores }, dispatch)
}
}
在这种情况下,您将能够通过组件中的this.props.loadProducts()创建操作.
