资讯专栏INFORMATION COLUMN

从头实现一个简易版React(三)

yvonne / 426人阅读

摘要:写在开头从头实现一个简易版二地址在上一节,我们的已经具备了渲染功能。参考资料,感谢几位前辈的分享陈屹深入技术栈

写在开头

从头实现一个简易版React(二)地址:https://segmentfault.com/a/11...
在上一节,我们的react已经具备了渲染功能。
在这一节我们将着重实现它的更新,说到更新,大家可能都会想到React的diff算法,它可以说是React性能高效的保证,同时也是最神秘,最难理解的部分(个人觉得),想当初我也是看了好多文章,敲了N次代码,调试了几十遍,才总算理解了它的大概。在这也算是把我的理解阐述出来。

进入正题

同样,我们会实现三种ReactComponent的update方法。不过在这之前,我们先想想,该如何触发React的更新呢?没错,就是setState方法。

</>复制代码

  1. // 所有自定义组件的父类
  2. class Component {
  3. constructor(props) {
  4. this.props = props
  5. }
  6. setState(newState) {
  7. this._reactInternalInstance.updateComponent(null, newState)
  8. }
  9. }
  10. //代码地址:src/react/Component.js

这里的reactInternalInstance就是我们在渲染ReactCompositeComponent时保存下的自身的实例,通过它调用了ReactCompositeComponent的update方法,接下来,我们就先实现这个update方法。

ReactCompositeComponent

这里的update方法同mount有点类似,都是调用生命周期和render方法,先上代码:

</>复制代码

  1. class ReactCompositeComponent extends ReactComponent {
  2. constructor(element) {
  3. super(element)
  4. // 存放对应的组件实例
  5. this._instance = null
  6. this._renderedComponent = null
  7. }
  8. mountComponent(rootId) {
  9. //内容略
  10. }
  11. // 更新
  12. updateComponent(nextVDom, newState) {
  13. // 如果有新的vDom,就使用新的
  14. this._vDom = nextVDom || this._vDom
  15. const inst = this._instance
  16. // 获取新的state,props
  17. const nextState = { ...inst.state, ...newState }
  18. const nextProps = this._vDom.props
  19. // 判断shouldComponentUpdate
  20. if (inst.shouldComponentUpdate && (inst.shouldComponentUpdate(nextProps, nextState) === false)) return
  21. inst.componentWillUpdate && inst.componentWillUpdate(nextProps, nextState)
  22. // 更改state,props
  23. inst.state = nextState
  24. inst.props = nextProps
  25. const prevComponent = this._renderedComponent
  26. // 获取render新旧的vDom
  27. const prevRenderVDom = prevComponent._vDom
  28. const nextRenderVDom = inst.render()
  29. // 判断是需要更新还是重新渲染
  30. if (shouldUpdateReactComponent(prevRenderVDom, nextRenderVDom)) {
  31. // 更新
  32. prevComponent.updateComponent(nextRenderVDom)
  33. inst.componentDidUpdate && inst.componentDidUpdate()
  34. } else {
  35. // 重新渲染
  36. this._renderedComponent = instantiateReactComponent(nextRenderVDom)
  37. // 重新生成对应的元素内容
  38. const nextMarkUp = this._renderedComponent.mountComponent(this._rootNodeId)
  39. // 替换整个节点
  40. $(`[data-reactid="${this._rootNodeId}"]`).replaceWith(nextMarkUp)
  41. }
  42. }
  43. }
  44. //代码地址:src/react/component/ReactCompositeComponent.js

有两点要说明:

熟悉React的都知道,很多时候组件的更新,vDom并没有变化,我们可以通过shouldComponentUpdate这个生命周期来优化这点,当shouldComponentUpdate为false时,直接return,不执行下面的代码。

当调用render获取到新的vDom时,将会比较新旧的vDom类型是否相同,这也属于diff算法优化的一部分,如果类型相同,则执行更新,反之,就重新渲染。

</>复制代码

  1. // 判断是更新还是渲染
  2. function shouldUpdateReactComponent(prevVDom, nextVDom) {
  3. if (prevVDom != null && nextVDom != null) {
  4. const prevType = typeof prevVDom
  5. const nextType = typeof nextVDom
  6. if (prevType === "string" || prevType === "number") {
  7. return nextType === "string" || nextType === "number"
  8. } else {
  9. return nextType === "object" && prevVDom.type === nextVDom.type && prevVDom.key === nextVDom.key
  10. }
  11. }
  12. }
  13. //代码地址:src/react/component/util.js

注意,这里我们使用到了key,当type相同时使用key可以快速准确得出两个vDom是否相同,这是为什么React要求我们在循环渲染时必须添加key这个props。

ReactTextComponent

ReactTextComponent的update方法非常简单,判断新旧文本是否相同,不同则更新内容,直接贴代码:

</>复制代码

  1. class ReactTextComponent extends ReactComponent {
  2. mountComponent(rootId) {
  3. //省略
  4. }
  5. // 更新
  6. updateComponent(nextVDom) {
  7. const nextText = "" + nextVDom
  8. if (nextText !== this._vDom) {
  9. this._vDom = nextText
  10. }
  11. // 替换整个节点
  12. $(`[data-reactid="${this._rootNodeId}"]`).html(this._vDom)
  13. }
  14. // 代码地址:src/react/component/ReactTextComponent.js
  15. }
ReactDomComponent

ReactDomComponent的update最复杂,可以说diff的核心都在这里,本文的重心也就放在这。
整个update分为两块,props的更新和children的更新。

</>复制代码

  1. class ReactDomComponent extends ReactComponent {
  2. mountComponent(rootId) {
  3. //省略
  4. }
  5. // 更新
  6. updateComponent(nextVDom) {
  7. const lastProps = this._vDom.props
  8. const nextProps = nextVDom.props
  9. this._vDom = nextVDom
  10. // 更新属性
  11. this._updateDOMProperties(lastProps, nextProps)
  12. // 再更新子节点
  13. this._updateDOMChildren(nextVDom.props.children)
  14. }
  15. // 代码地址:src/react/component/ReactDomComponent.js
  16. }

props的更新非常简单,无非就是遍历新旧props,删除不在新props里的老props,添加不在老props里的新props,更新新旧都有的props,事件特殊处理。

</>复制代码

  1. _updateDOMProperties(lastProps, nextProps) {
  2. let propKey = ""
  3. // 遍历,删除已不在新属性集合里的老属性
  4. for (propKey in lastProps) {
  5. // 属性在原型上或者新属性里有,直接跳过
  6. if (nextProps.hasOwnProperty(propKey) || !lastProps.hasOwnProperty(propKey)) {
  7. continue
  8. }
  9. // 对于事件等特殊属性,需要多带带处理
  10. if (/^on[A-Za-z]/.test(propKey)) {
  11. const eventType = propKey.replace("on", "")
  12. // 针对当前的节点取消事件代理
  13. $(document).undelegate(`[data-reactid="${this._rootNodeId}"]`, eventType, lastProps[propKey])
  14. continue
  15. }
  16. }
  17. // 对于新的属性,需要写到dom节点上
  18. for (propKey in nextProps) {
  19. // 更新事件属性
  20. if (/^on[A-Za-z]/.test(propKey)) {
  21. var eventType = propKey.replace("on", "")
  22. // 以前如果已经有,需要先去掉
  23. lastProps[propKey] && $(document).undelegate(`[data-reactid="${this._rootNodeId}"]`, eventType, lastProps[propKey])
  24. // 针对当前的节点添加事件代理
  25. $(document).delegate(`[data-reactid="${this._rootNodeId}"]`, `${eventType}.${this._rootNodeId}`, nextProps[propKey])
  26. continue
  27. }
  28. if (propKey === "children") continue
  29. // 更新普通属性
  30. $(`[data-reactid="${this._rootNodeId}"]`).prop(propKey, nextProps[propKey])
  31. }
  32. }
  33. // 代码地址:src/react/component/ReactDomComponent.js

children的更新则相对复杂了很多,陈屹老师的《深入React技术栈》中提到,diff算法分为3块,分别是

tree diff

component diff

element diff

上文中的shouldUpdateReactComponent就属于component diff,接下来,让我们依据这三种diff实现updateChildren。

</>复制代码

  1. // 全局的更新深度标识,用来判定触发patch的时机
  2. let updateDepth = 0
  3. // 全局的更新队列
  4. let diffQueue = []
  5. _updateDOMChildren(nextChildVDoms) {
  6. updateDepth++
  7. // diff用来递归查找差异,组装差异对象,并添加到diffQueue中
  8. this._diff(diffQueue, nextChildVDoms)
  9. updateDepth--
  10. if (updateDepth === 0) {
  11. // 具体的dom渲染
  12. this._patch(diffQueue)
  13. diffQueue = []
  14. }

这里通过updateDepth对vDom树进行层级控制,只会对相同层级的DOM节点进行比较,只有当一棵DOM树全部遍历完,才会调用patch处理差异。也就是所谓的tree diff。
确保了同层次后,我们要实现_diff方法。
已经渲染过的子ReactComponents在这里是数组,我们要遍历出里面的vDom进行比较,这里就牵扯到上文中的key,在有key时,我们优先用key来获取vDom,所以,我们首先遍历数组,将其转为map(这里先用object代替,以后会更改成es6的map),如果有key值的,就用key值作标识,无key的,就用index。
下面是array到map的代码:

</>复制代码

  1. // 将children数组转化为map
  2. export function arrayToMap(array) {
  3. array = array || []
  4. const childMap = {}
  5. array.forEach((item, index) => {
  6. const name = item && item._vDom && item._vDom.key ? item._vDom.key : index.toString(36)
  7. childMap[name] = item
  8. })
  9. return childMap
  10. }

部分diff方法:

</>复制代码

  1. // 将之前子节点的component数组转化为map
  2. const prevChildComponents = arrayToMap(this._renderedChildComponents)
  3. // 生成新的子节点的component对象集合
  4. const nextChildComponents = generateComponentsMap(prevChildComponents, nextChildVDoms)

将ReactComponent数组转化为map后,用老的ReactComponents集合和新vDoms数组生成新的ReactComponents集合,这里会使用shouldUpdateReactComponent进行component diff,如果相同,则直接更新即可,反之,就重新生成ReactComponent

</>复制代码

  1. /**
  2. * 用来生成子节点的component
  3. * 如果是更新,就会继续使用以前的component,调用对应的updateComponent
  4. * 如果是新的节点,就会重新生成一个新的componentInstance
  5. */
  6. function generateComponentsMap(prevChildComponents, nextChildVDoms = []) {
  7. const nextChildComponents = {}
  8. nextChildVDoms.forEach((item, index) => {
  9. const name = item.key ? item.key : index.toString(36)
  10. const prevChildComponent = prevChildComponents && prevChildComponents[name]
  11. const prevVdom = prevChildComponent && prevChildComponent._vDom
  12. const nextVdom = item
  13. // 判断是更新还是重新渲染
  14. if (shouldUpdateReactComponent(prevVdom, nextVdom)) {
  15. // 更新的话直接递归调用子节点的updateComponent
  16. prevChildComponent.updateComponent(nextVdom)
  17. nextChildComponents[name] = prevChildComponent
  18. } else {
  19. // 重新渲染的话重新生成component
  20. const nextChildComponent = instantiateReactComponent(nextVdom)
  21. nextChildComponents[name] = nextChildComponent
  22. }
  23. })
  24. return nextChildComponents
  25. }

经历了以上两步,我们已经获得了新旧同层级的ReactComponents集合。需要做的,只是遍历这两个集合,进行比较,同属性的更新一样,进行移动,新增,和删除,当然,在这个过程中,我会包含我们的第三种优化,element diff。它的策略是这样的:首先对新集合的节点进行循环遍历,通过唯一标识可以判断新老集合中是否存在相同的节点,如果存在相同节点,则进行移动操作,但在移动前需要将当前节点在老集合中的位置与 lastIndex 进行比较,if (prevChildComponent._mountIndex < lastIndex),则进行节点移动操作,否则不执行该操作。这是一种顺序优化手段,lastIndex 一直在更新,表示访问过的节点在老集合中最右的位置(即最大的位置),如果新集合中当前访问的节点比 lastIndex 大,说明当前访问节点在老集合中就比上一个节点位置靠后,则该节点不会影响其他节点的位置,因此不用添加到差异队列中,即不执行移动操作,只有当访问的节点比 lastIndex 小时,才需要进行移动操作。
上完整的diff方法代码:

</>复制代码

  1. // 差异更新的几种类型
  2. const UPDATE_TYPES = {
  3. MOVE_EXISTING: 1,
  4. REMOVE_NODE: 2,
  5. INSERT_MARKUP: 3
  6. }
  7. // 追踪差异
  8. _diff(diffQueue, nextChildVDoms) {
  9. // 将之前子节点的component数组转化为map
  10. const prevChildComponents = arrayToMap(this._renderedChildComponents)
  11. // 生成新的子节点的component对象集合
  12. const nextChildComponents = generateComponentsMap(prevChildComponents, nextChildVDoms)
  13. // 重新复制_renderChildComponents
  14. this._renderedChildComponents = []
  15. for (let name in nextChildComponents) {
  16. nextChildComponents.hasOwnProperty(name) && this._renderedChildComponents.push(nextChildComponents[name])
  17. }
  18. let lastIndex = 0 // 代表访问的最后一次老的集合位置
  19. let nextIndex = 0 // 代表到达的新的节点的index
  20. // 通过对比两个集合的差异,将差异节点添加到队列中
  21. for (let name in nextChildComponents) {
  22. if (!nextChildComponents.hasOwnProperty(name)) continue
  23. const prevChildComponent = prevChildComponents && prevChildComponents[name]
  24. const nextChildComponent = nextChildComponents[name]
  25. // 相同的话,说明是使用的同一个component,需要移动
  26. if (prevChildComponent === nextChildComponent) {
  27. // 添加差异对象,类型:MOVE_EXISTING
  28. prevChildComponent._mountIndex < lastIndex && diffQueue.push({
  29. parentId: this._rootNodeId,
  30. parentNode: $(`[data-reactid="${this._rootNodeId}"]`),
  31. type: UPDATE_TYPES.MOVE_EXISTING,
  32. fromIndex: prevChildComponent._mountIndex,
  33. toIndex: nextIndex
  34. })
  35. lastIndex = Math.max(prevChildComponent._mountIndex, lastIndex)
  36. } else {
  37. // 如果不相同,说明是新增的节点
  38. // 如果老的component在,需要把老的component删除
  39. if (prevChildComponent) {
  40. diffQueue.push({
  41. parentId: this._rootNodeId,
  42. parentNode: $(`[data-reactid="${this._rootNodeId}"]`),
  43. type: UPDATE_TYPES.REMOVE_NODE,
  44. fromIndex: prevChildComponent._mountIndex,
  45. toIndex: null
  46. })
  47. // 去掉事件监听
  48. if (prevChildComponent._rootNodeId) {
  49. $(document).undelegate(`.${prevChildComponent._rootNodeId}`)
  50. }
  51. lastIndex = Math.max(prevChildComponent._mountIndex, lastIndex)
  52. }
  53. // 新增加的节点
  54. diffQueue.push({
  55. parentId: this._rootNodeId,
  56. parentNode: $(`[data-reactid="${this._rootNodeId}"]`),
  57. type: UPDATE_TYPES.INSERT_MARKUP,
  58. fromIndex: null,
  59. toIndex: nextIndex,
  60. markup: nextChildComponent.mountComponent(`${this._rootNodeId}.${name}`)
  61. })
  62. }
  63. // 更新_mountIndex
  64. nextChildComponent._mountIndex = nextIndex
  65. nextIndex++
  66. }
  67. // 对于老的节点里有,新的节点里没有的,全部删除
  68. for (let name in prevChildComponents) {
  69. const prevChildComponent = prevChildComponents[name]
  70. if (prevChildComponents.hasOwnProperty(name) && !(nextChildComponents && nextChildComponents.hasOwnProperty(name))) {
  71. diffQueue.push({
  72. parentId: this._rootNodeId,
  73. parentNode: $(`[data-reactid="${this._rootNodeId}"]`),
  74. type: UPDATE_TYPES.REMOVE_NODE,
  75. fromIndex: prevChildComponent._mountIndex,
  76. toIndex: null
  77. })
  78. // 如果渲染过,去掉事件监听
  79. if (prevChildComponent._rootNodeId) {
  80. $(document).undelegate(`.${prevChildComponent._rootNodeId}`)
  81. }
  82. }
  83. }
  84. }
  85. // 代码地址:src/react/component/ReactDomCompoent.js

调用diff方法后,会回到tree diff那一步,当一整棵树遍历完后,就需要通过Patch将更新的内容渲染出来了,patch方法相对比较简单,由于我们把更新的内容都放入了diffQueue中,只要遍历这个数组,根据不同的类型进行相应的操作就行。

</>复制代码

  1. // 渲染
  2. _patch(updates) {
  3. // 处理移动和删除的
  4. updates.forEach(({ type, fromIndex, toIndex, parentNode, parentId, markup }) => {
  5. const updatedChild = $(parentNode.children().get(fromIndex))
  6. switch (type) {
  7. case UPDATE_TYPES.INSERT_MARKUP:
  8. insertChildAt(parentNode, $(markup), toIndex) // 插入
  9. break
  10. case UPDATE_TYPES.MOVE_EXISTING:
  11. deleteChild(updatedChild) // 删除
  12. insertChildAt(parentNode, updatedChild, toIndex)
  13. break
  14. case UPDATE_TYPES.REMOVE_NODE:
  15. deleteChild(updatedChild)
  16. break
  17. default:
  18. break
  19. }
  20. })
  21. }
  22. // 代码地址:src/react/component/ReactDomComponent.js
总结

以上,整个简易版React就完成了,可以试着写些简单的例子跑跑看了,是不是非常有成就感呢?

总结下更新:
ReactCompositeComponent:负责调用生命周期,通过component diff将更新都交给了子ReactComponet
ReactTextComponent:直接更新内容
ReactDomComponent:先更新props,在更新children,更新children分为三步,tree diff保证同层级比较,使用shouldUpdateReactComponent进行component diff,最后在element diff通过lastIndex顺序优化

至此,整个从头实现简易版React就结束了,感谢大家的观看。

参考资料,感谢几位前辈的分享:
https://www.cnblogs.com/sven3...
https://github.com/purplebamb...
陈屹 《深入React技术栈》

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/93134.html

相关文章

  • 从头实现一个简易React(一)

    摘要:既然看不懂,那就看看社区前辈们写的一些源码分析文章以及实现思路吧,又这么过了几天,总算是摸清点思路,于是在参考了前辈们的基础上,实现了一个简易版的。总结以上就是实现一个的总体思路,下节我们重点放在不同的上。 写在开头 工作中使用react也很长一段时间了,虽然对它的用法,原理有了一定的了解,但是总感觉停留在表面。本着知其然知其所以然的态度,我试着去看了react源码,几天下来,发现并不...

    meislzhua 评论0 收藏0
  • 从头实现一个简易React(二)

    摘要:写在开头从头实现一个简易版一地址上一节,我们详细介绍了实现一个简易的思路以及整体的结构,但是对于渲染和更新的原理,却还没有提及,因此,本节我们将重点放在的渲染上。 写在开头 从头实现一个简易版React(一)地址:https://segmentfault.com/a/11...上一节,我们详细介绍了实现一个简易React的思路以及整体的结构,但是对于渲染和更新的原理,却还没有提及,因此...

    vvpvvp 评论0 收藏0
  • 基于react native的登录界面demo 超简易教程 redux

    摘要:登录视图登陆失败用户名或密码不能为空弹出提示框成功是点击登录按钮后调用的函数,这里的功能比较简单。通过把发出去密码登录声明组件需要整个中的哪一部分数据作为自己的将和组件联系在一起编写是负责生成的,所以在大项目中还会用到合并。 本猪说 本猪猪刚学react,也刚看RN,就叫写这个,苦不堪言,搭环境就搭了好久。看网上教程也是改了好多小地方才写完了。本着雷锋精神手把手教你写(假的)。 sho...

    scq000 评论0 收藏0
  • 关于Vue2一些值得推荐的文章 -- 五、六月份

    摘要:五六月份推荐集合查看最新的请点击集前端最近很火的框架资源定时更新,欢迎一下。苏幕遮燎沈香宋周邦彦燎沈香,消溽暑。鸟雀呼晴,侵晓窥檐语。叶上初阳乾宿雨,水面清圆,一一风荷举。家住吴门,久作长安旅。五月渔郎相忆否。小楫轻舟,梦入芙蓉浦。 五、六月份推荐集合 查看github最新的Vue weekly;请::点击::集web前端最近很火的vue2框架资源;定时更新,欢迎 Star 一下。 苏...

    sutaking 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<