微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

React-Redux源码剖析

React-Redux是用在连接React和Redux上的。如果你想同时用这两个框架,那么React-Redux基本就是必须的了。为了能够更好的使用这个工具,今天就对它进行一下源码剖析。

Provider

一个React组件,一般你的rootApp要放倒这个组件内部渲染。它很简单,最关键的作用就是在context中放入Redux的store,方便子组件获取。关键代码

getChildContext() {
    return { store: this.store }
}

Provider.childContextTypes = {
   store: storeShape.isrequired
}

这样connect的组件就可以获取store,使用store的方法

connect

首选connect是个可以执行两次的柯里化函数,第一次传入的参数相当于一系列的定制化东西,第二次传入的是你要连接的React组件,然后返回一个新的React组件。
第一次执行时传入的参数是mapStatetoProps,mapdispatchToProps,mergeProps,options这四个。首先会对这几个参数进行处理,代码如下:

//决定组件会不会因state改变而更新
const shouldSubscribe = Boolean(mapStatetoProps)
//如果不传递这个参数使用认state => ({})
const mapState = mapStatetoProps || defaultMapStatetoProps

//mapdispatchToProps的处理,最后的情况实际是使用bindActionCreators处理
let mapdispatch
if (typeof mapdispatchToProps === 'function') {
    mapdispatch = mapdispatchToProps
} else if (!mapdispatchToProps) {
    mapdispatch = defaultMapdispatchToProps
} else {
    mapdispatch = wrapActionCreators(mapdispatchToProps)
}

//不传递就使用认值
const finalMergeProps = mergeProps || defaultMergeProps
const { pure = true,withRef = false } = options

第二次执行函数接收的参数是个React组件:WrappedComponent,之后返回一个新的React组件Connect。

return hoistStatics(Connect,WrappedComponent)

把WrappedComponent的非React属性拷贝到Connect上。下面详细说下Connect。

Connect

一个React组件

Connect.contextTypes = {
    store: storeShape
}

所以它可以从context中获取Provider放的store。

constructor

在constructor中:

//获取store
this.store = props.store || context.store
const storeState = this.store.getState()
//把store的state作为组件的state,后面通过更新state更新组件
this.state = { storeState }
//清除组件的状态,内部是一系列的标示还原
this.clearCache()

render

然后是render方法,在挂载的时候,会经过一系列的判断和计算,比如使用mapState计算nextStateProps,并和this.stateProps对比是否发生改变,如果发生改变:

nextdispatchProps = mapState(store.getState(),[props])
this.stateProps = nextdispatchProps

使用mapdispatch计算nextdispatchProps,并和this.dispatchProps对比是否发生改变,如果发生改变:

nextMergedProps = mapdispatch(dispatch,[props])
this.dispatchProps = nextMergedProps

如果上面的两个对比有一个发生改变,就会继续使用finalMergeProps来计算最终的数据合并结果nextMergedProps,并和this.mergedProps对比是否发生改变,如果发生改变:

nextMergedProps = finalMergeProps(this.stateProps,this.dispatchProps,this.props)
this.mergedProps = nextMergedProps

如果上面的对比确定发生改变

if (withRef) {
  this.renderedElement = createElement(WrappedComponent,{
      ...this.mergedProps,ref: 'wrappedInstance'
  })
 } else {
  this.renderedElement = createElement(WrappedComponent,this.mergedProps
  )
 }
  return this.renderedElement

如果withRef等于true就会增加ref属性,然后可以通过getWrappedInstance方法获取DOM。如果前面说的这些对比的结果都是false,就会直接返回this.renderedElement,组件不进行任何更新。当然组件挂载的时候前面的对比都会返回true。

componentDidMount

它内部的关键代码是:

if (shouldSubscribe && !this.unsubscribe) {
    this.unsubscribe = this.store.subscribe(this.handleChange.bind(this))
    this.handleChange()
}

在不指定mapStatetoProps的时候shouldSubscribe等于false,这就意味着React-Redux的源码剖析到此结束,谢谢观看!当然如果指定了mapStatetoProps剖析就还得继续。看到代码没有,竟然使用subscribe,意味着只要执行dispatch,handleChange就会执行。至此组件已经挂载完毕,后面的代码执行需要有外界因素了,比如父组件传递新的props、执行dispatch。

componentwillReceiveProps

组件还实现了componentwillReceiveProps这个React生命周期中的方法

componentwillReceiveProps(nextProps) {
    if (!pure || !shallowEqual(nextProps,this.props)) {
        this.haveOwnPropsChanged = true
    }
}

看到pure的重要性了吧,如果pure被设置为false就意味着不管属性是否浅相等this.haveOwnPropsChanged总是会被设置为true,而这会导致后面一系列的为了更新而进行的计算,所以pure为true是可以给你的性能带来帮助的,不过它认就是true。这里设置this.haveOwnPropsChanged等于true是给通过直接通过父组件传递props更新组件带来可能,当然需要配合mapStatetoProps,mergeProps这三个函数,如果它们都没有利用ownProps,最终组件还是不能通过这种方式更新。

handleChange

下面假定触发了一次dispatch,这个时候handleChange就会执行,如果state没有发生改变,并且pure为true,就什么都不做直接返回,pure又在性能上立功了。如果state发生了改变会再做一些计算对比,比如计算this.stateProps。最后是在要更新的时候会:

this.hasstoreStateChanged = true
this.setState({ storeState })

调用setState来触发组件更新。这里其实意味着只要store的state发生改变,所有的mapStatetoProps、 mapdispatchToProps、mergeProps都会执行。

shouldComponentUpdate

这个时候会调用它内部实现的shouldComponentUpdate,用来提高性能

shouldComponentUpdate() {
    return !pure || this.haveOwnPropsChanged || this.hasstoreStateChanged
}

但是怎么感觉这个并没有什么用呢?可能是我理解不深,因为无论是父组件更新props还是state改变这里总是返回true,而不管改变的是不是这个组件关心的数据。没办法又进入了render方法

好了,源码剖析到此结束,谢谢观看!

原文地址:https://www.jb51.cc/react/306277.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐