我对原生的反应相当新,我想知道是否有可能将单个屏幕组件连接到mapstatetoProp函数中的多个reducer。请注意,我使用redux进行状态管理。
答案 0 :(得分:1)
这是mapDispatchToProps和mapStateToProps的一个简单示例。
// Here you can import multiple actions for multiple reducers
import ProductActions from '...'
import BasketActions from '...'
class MyScreen extends Component () {
_addUser (user) {
this.props.addUser(user)
}
_addProduct (product) {
this.props.addProduct(product)
}
render () {
// Here you can use all props that you mapped in mapStateToProps
}
}
const mapDispatchToProps = dispatch => ({
addProduct: (product) => dispatch(ProductActions.add(product)),
addUser: (user) => dispatch(BasketActions.setInstruction(user))
})
const mapStateToProps = (state, ownProps) => {
products: state.products,
users: basketItemsSelector(state, restaurant.id)
}
export default connect(mapStateToProps, mapDispatchToProps)(MyScreen)