加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-redux

发布时间:2020-12-15 05:18:27 所属栏目:百科 来源:网络整理
导读:react-redux是react和redux的绑定库, react-redux提供了两个模块: provider:app顶层容器,接受Redux的store作为props,主要是确保store被connect访问到 connect:连接react组件到store,返回一个包含store和react组件的对象 connect([mapStateToProps],[m

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读