【问题标题】:How to share state between components using Redux?如何使用 Redux 在组件之间共享状态?
【发布时间】:2019-12-10 22:20:05
【问题描述】:
我有两个组件,父母和孩子。我想使用 redux 存储子组件的状态,并在父组件或应用程序的任何位置访问它。
有人可以帮我解决这个问题吗?谢谢
【问题讨论】:
标签:
javascript
reactjs
ecmascript-6
redux
react-redux
【解决方案2】:
以下两个步骤是......
步骤01:使用redux like设置props值
jsx:
<button onClick={()=>this.props.AddProductToCart(this.state.quantity)} className='btn btn-sm btn-success'>Add to Cart</button>
const mapStateToProps=(state)=>({
quantity:state.CartQtyManageReducer.quantity
})
export default connect(mapStateToProps,{AddProductToCart})(AddToCart)
步骤02:从redux状态获取LayoutNavbar中的props
const mapStateToProps=(state)=>({
quantity:state.CartQtyManageReducer.quantity
})
export default connect(mapStateToProps,{})(LayoutNavbar)