如何在 react-redux 的 mapDispatchToProps 中分派多个动作
How to dispatch more than one action in mapDispatchToProps in react-redux
mapDispatchToProps 中共有三个函数。我想在 React Component 的构造函数中使用它们的任何函数,但是当我使用 console.log(this.props)
时,它给出了未定义的我如何在构造函数中使用这些函数从 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);
}
报错
Uncaught TypeError: Cannot read property 'loadProducts' of undefined
您将使用 redux
、f.e 中的 bindActionCreators
。:
const mapDispatchToProps = (dispatch) => {
return {
...bindActionCreators({ loadProducts, loadStores }, dispatch)
}
}
在这种情况下,您将能够通过 this.props.loadProducts()
在组件中创建动作。
现在可以使用了,我忘了在构造函数中传递道具
constructor(props) {
super();
this.state = {
products: [],
stores: [],
currentProduct: [],
stockAvailable: [],
productName: '',
description: '',
qty: 0,
unitPrice: 0,
storeName: '',
selectedProduct: '',
productNameInStock: '',
productQtyInStock:0
}
console.log(props)
props.loadProducts();
props.loadStores();
this.submit = this.submit.bind(this);
this.inputHandler = this.inputHandler.bind(this);
}
mapDispatchToProps 中共有三个函数。我想在 React Component 的构造函数中使用它们的任何函数,但是当我使用 console.log(this.props)
时,它给出了未定义的我如何在构造函数中使用这些函数从 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);
}
报错
Uncaught TypeError: Cannot read property 'loadProducts' of undefined
您将使用 redux
、f.e 中的 bindActionCreators
。:
const mapDispatchToProps = (dispatch) => {
return {
...bindActionCreators({ loadProducts, loadStores }, dispatch)
}
}
在这种情况下,您将能够通过 this.props.loadProducts()
在组件中创建动作。
现在可以使用了,我忘了在构造函数中传递道具
constructor(props) {
super();
this.state = {
products: [],
stores: [],
currentProduct: [],
stockAvailable: [],
productName: '',
description: '',
qty: 0,
unitPrice: 0,
storeName: '',
selectedProduct: '',
productNameInStock: '',
productQtyInStock:0
}
console.log(props)
props.loadProducts();
props.loadStores();
this.submit = this.submit.bind(this);
this.inputHandler = this.inputHandler.bind(this);
}