上篇provider
connect
:连接react组件和react store的函数
提供两个函数,其一是
mapStateProps
,为组件提供了store中的数据,另一个是mapDispatchToProps
将操作分派给store。
function connect(mapStateToProps?, mapDispatchToProps?,
mergeProps?, options?)
参数解释:
- mapStateToProps(state, ownProps):监听store值的变化,返回一个纯对象,与组件中的props值合并,
- mapDispatchToProps
- mergeProps:
- options(object);pure默认值为true。
connect工作流程
- 从context获取store
- 在
componentWillMount
中通过mapStateToProps获取state的值 - 在
componentWillMount
中通过mapDispatchToProps中获取dispatch的值 - 从
componentWillMount
中订阅store的变化 - 将获得的state、dispatch 和自身的props传给下一个组件