将屏幕组件连接到多个reducer的问题

时间:2018-04-08 17:00:26

标签: react-native react-redux

我对原生的反应相当新,我想知道是否有可能将单个屏幕组件连接到mapstatetoProp函数中的多个reducer。请注意,我使用redux进行状态管理。

1 个答案:

答案 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)