react-redux

react-redux是react和redux的绑定库,
react-redux提供了两个模块:

  • provider:app顶层容器,接受Redux的store作为props,主要是确保store被connect访问到
  • connect:连接react组件到store,返回一个包含store和react组件的对象

connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])
- mapStateToProps:返回需要合并进props的state,每次更新时,返回一个最新的state对象,若省略该步,组件将不会订阅store
- mapDispatchToProps:返回dispatch的actions,通过bindActionCreators使用dispatch将action creator 包围起来,生成同名key对象
- mergeProps(stateProps, dispatchProps, ownProps):合并mapStateToProps(),mapDispatchToProps()和parent props,其结果对象作为props传给被包装的组件
- options:pure,是否打开优化,实现了shouldComponentUpdate和浅比较 mergeProps,阻止不必要的更新,确认组件的纯粹性,不依赖除props,store state的任何输出和状态;withref,给包装在里面的组件实例提供一个ref,可通过getWrappedInstance()获取

Provider继承Component

class Provider extends Component {
  getChildContext() {
    return { store: this.store }
  }

  constructor(props, context) {
    super(props, context)
    this.store = props.store
  }

  render() {
    const { children } = this.props
    return Children.only(children)
  }
}
Provider.propTypes = {
  store: storeShape.isRequired,
  children: PropTypes.element.isRequired
}//store,children
Provider.childContextTypes = {
  store: storeShape.isRequired
}//store

connect:返回一个WrappedComponent对象,做四件事

  1. 计算StateProps
  2. 计算DispatchProps
  3. MergedProps
  4. 判断是否update

你可能感兴趣的:(react,redux,绑定库)