connect

使用 connect 使组件和 Store 连接

  1. connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])

mapStateToProps

允许我们将 store 中的数据绑定到 props 上

函数的第一个参数就是 store(redux 中的 store) ,ownProps 组件自身的 props

function mapStateToProps(store,ownProps){
    return {count:store.count}
}

当 state 变化,或者 ownProps 变化的时候,mapStateToProps 都会被调用,计算出一个新的 stateProps,(在与 ownProps merge 后)更新给 MyComp。

cmapDispatchToProps

它的功能是,将 action 作为 props 绑定到 MyComp 上。

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
    increase: (...args) => dispatch(actions.increase(...args)),
    decrease: (...args) => dispatch(actions.decrease(...args))
  }
}