gpt4 book ai didi

reactjs - 如何在react-redux中的mapDispatchToProps中调度多个 Action

转载 作者:行者123 更新时间:2023-12-03 14:00:15 24 4
gpt4 key购买 nike

mapDispatchToProps 中有三个函数。我想在 React 组件的构造函数中使用它们中的任何函数,但是当我使用 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 中的 bindActionCreators,例如:

const mapDispatchToProps = (dispatch) => {
return {
...bindActionCreators({ loadProducts, loadStores }, dispatch)
}
}

在这种情况下,您将能够通过组件中的 this.props.loadProducts() 创建操作。

关于reactjs - 如何在react-redux中的mapDispatchToProps中调度多个 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43450665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com