资讯专栏INFORMATION COLUMN

React 源码深度解读(二):首次 DOM 元素渲染 - Part 2

wean / 848人阅读

摘要:本文将要讲解的调用栈是下面这个样子的平台无关相关如果看源码,我们会留意到很多相关的代码,我们暂时先将其忽略,会在后续的文章中进行讲解。现在我们来看一下各实例间的关系目前为止的调用栈平台无关相关下一篇讲解三总结本文讲解了转化为的过程。

欢迎关注我的公众号睿Talk,获取我最新的文章:

一、前言

React 是一个十分庞大的库,由于要同时考虑 ReactDom 和 ReactNative ,还有服务器渲染等,导致其代码抽象化程度很高,嵌套层级非常深。阅读 React 源码是一个非常艰辛的过程,在学习过程中给我帮助最大的就是这个系列文章。作者对代码的调用关系梳理得非常清楚,而且还有配图帮助理解,非常值得一读。站在巨人的肩膀之上,我尝试再加入自己的理解,希望对有志于学习 React 源码的读者带来一点启发。

本系列文章基于 React 15.4.2 ,以下是本系列其它文章的传送门:
React 源码深度解读(一):首次 DOM 元素渲染 - Part 1
React 源码深度解读(二):首次 DOM 元素渲染 - Part 2
React 源码深度解读(三):首次 DOM 元素渲染 - Part 3
React 源码深度解读(四):首次自定义组件渲染 - Part 1
React 源码深度解读(五):首次自定义组件渲染 - Part 2
React 源码深度解读(六):依赖注入
React 源码深度解读(七):事务 - Part 1
React 源码深度解读(八):事务 - Part 2
React 源码深度解读(九):单个元素更新
React 源码深度解读(十):Diff 算法详解

二、从 ReactCompositeComponent 到 ReactDOMComponent

上一篇文章中,介绍了顶层对象ReactCompositeComponent[T]是如何构造的,接下来我们看看 batchedMountComponentIntoNode 做了什么事情。

本文将要讲解的调用栈是下面这个样子的:

</>复制代码

  1. |=ReactMount.render(nextElement, container, callback) ___
  2. |=ReactMount._renderSubtreeIntoContainer() |
  3. |-ReactMount._renderNewRootComponent() |
  4. |-instantiateReactComponent() |
  5. |~batchedMountComponentIntoNode() upper half
  6. |~mountComponentIntoNode() (平台无关)
  7. |-ReactReconciler.mountComponent() |
  8. |-ReactCompositeComponent.mountComponent() |
  9. |-ReactCompositeComponent.performInitialMount() |
  10. |-instantiateReactComponent() _|_
  11. |-ReactDOMComponent.mountComponent() lower half
  12. |-_mountImageIntoNode() (HTML DOM 相关)
  13. _|_

如果看源码,我们会留意到很多transaction相关的代码,我们暂时先将其忽略,会在后续的文章中进行讲解。暂时可以理解为调用transaction.perform时,实际上就是对第一个参数进行函数调用。跳过一些模版代码后,实际上做事情的是 mountComponentIntoNode 这个方法。

</>复制代码

  1. // 文件位置:src/renderers/dom/client/ReactMount.js
  2. function mountComponentIntoNode(
  3. wrapperInstance, // ReactCompositeComponent[T]
  4. container, // document.getElementById("root")
  5. transaction,
  6. shouldReuseMarkup,
  7. context
  8. ) {
  9. ...
  10. var markup = ReactReconciler.mountComponent(
  11. wrapperInstance,
  12. transaction,
  13. null,
  14. ReactDOMContainerInfo(wrapperInstance, container),
  15. context,
  16. 0 /* parentDebugID */
  17. );
  18. ...
  19. ReactMount._mountImageIntoNode(
  20. markup,
  21. container,
  22. wrapperInstance,
  23. shouldReuseMarkup,
  24. transaction
  25. );
  26. }

ReactReconciler.mountComponent 用于创建 DOM 元素,而 ReactMount._mountImageIntoNode 则是将刚创建的 DOM 元素挂载到页面。ReactReconciler.mountComponent 会调用 ReactCompositeComponent[T]的 mountComponent 方法。在看 mountComponent 方法前,还需要先准备好 hostContainerInfo,由 ReactDOMContainerInfo 生成:

</>复制代码

  1. // 文件位置:src/renderers/dom/shared/ReactDOMContainerInfo.js
  2. function ReactDOMContainerInfo(
  3. topLevelWrapper, // ReactCompositeComponent[T]
  4. node // document.getElementById("root")
  5. ) {
  6. var info = {
  7. _topLevelWrapper: topLevelWrapper,
  8. _idCounter: 1,
  9. _ownerDocument: node ?
  10. node.nodeType === DOC_NODE_TYPE ? node : node.ownerDocument : null,
  11. _node: node,
  12. _tag: node ? node.nodeName.toLowerCase() : null,
  13. _namespaceURI: node ? node.namespaceURI : null,
  14. };
  15. ...
  16. return info;
  17. }

现在各实例间的关系是这样的:

再继续看 mountComponent 方法:

</>复制代码

  1. // 文件位置:src/renderers/shared/stack/reconciler/ReactCompositeComponent.js
  2. mountComponent: function (
  3. transaction,
  4. hostParent,
  5. hostContainerInfo,
  6. context
  7. ) {
  8. ...
  9. // this._currentElement 为ReactElement[2](TopLevelWrapper)
  10. var publicProps = this._currentElement.props;
  11. var publicContext = this._processContext(context);
  12. // TopLevelWrapper
  13. var Component = this._currentElement.type;
  14. ...
  15. // Initialize the public class
  16. var doConstruct = shouldConstruct(Component);
  17. // 生成TopLevelWrapper 实例
  18. var inst = this._constructComponent(
  19. doConstruct,
  20. publicProps,
  21. publicContext,
  22. updateQueue
  23. );
  24. ...
  25. this._instance = inst;
  26. ...
  27. var markup;
  28. ...
  29. markup = this.performInitialMount(renderedElement,
  30. hostParent, hostContainerInfo, transaction, context
  31. ...
  32. return markup;
  33. },
  34. performInitialMount: function (renderedElement, hostParent,
  35. hostContainerInfo, transaction, context) {
  36. // TopLevelWrapper 实例
  37. var inst = this._instance;
  38. ...
  39. // If not a stateless component, we now render
  40. if (renderedElement === undefined) {
  41. // 返回值为 ReactElement[1]
  42. renderedElement = this._renderValidatedComponent();
  43. }
  44. // 返回 ReactNodeTypes.HOST
  45. var nodeType = ReactNodeTypes.getType(renderedElement);
  46. this._renderedNodeType = nodeType;
  47. // instantiateReactComponent.js
  48. var child = this._instantiateReactComponent(
  49. renderedElement,
  50. nodeType !== ReactNodeTypes.EMPTY /* shouldHaveDebugID */
  51. );
  52. this._renderedComponent = child;
  53. var markup = ReactReconciler.mountComponent(
  54. child,
  55. transaction,
  56. hostParent,
  57. hostContainerInfo,
  58. this._processChildContext(context),
  59. debugID
  60. );
  61. ...
  62. return markup;
  63. },

当运行到var child = this._instantiateReactComponent时,就会调用上篇文章说到的instantiateReactComponent文件:

</>复制代码

  1. // 文件位置:src/renderers/shared/stack/reconciler/instantiateReactComponent.js
  2. function instantiateReactComponent(node, shouldHaveDebugID) {
  3. var instance;
  4. ...
  5. } else if (typeof node === "object") {
  6. ...
  7. // element.type 为 ‘h1’
  8. if (typeof element.type === "string") {
  9. instance = ReactHostComponent.createInternalComponent(element);
  10. }
  11. return instance;
  12. }

ReactDom 会在执行的时候,执行ReactDefaultInjection.inject()将 ReactDOMComponent 注入到 ReactHostComponent 中,ReactHostComponent.createInternalComponent 最终会调用 ReactDOMComponent:

</>复制代码

  1. // 文件位置:src/renderers/dom/shared/ReactDomComponent.js
  2. function ReactDOMComponent(element) {
  3. // h1
  4. var tag = element.type;
  5. validateDangerousTag(tag);
  6. // ReactElement[1]
  7. this._currentElement = element;
  8. this._tag = tag.toLowerCase();
  9. this._namespaceURI = null;
  10. this._renderedChildren = null;
  11. this._previousStyle = null;
  12. this._previousStyleCopy = null;
  13. this._hostNode = null;
  14. this._hostParent = null;
  15. this._rootNodeID = 0;
  16. this._domID = 0;
  17. this._hostContainerInfo = null;
  18. this._wrapperState = null;
  19. this._topLevelWrapper = null;
  20. this._flags = 0;
  21. }

我们将返回的实例命名为 ReactDOMComponent[ins]。

ReactReconciler.mountComponent 会调用 ReactDomComponent 的 mountComponent 方法,这就会涉及到 HTML DOM 相关的内容,我们在下一篇进行讲解。

现在我们来看一下各实例间的关系:

目前为止的调用栈:

</>复制代码

  1. |=ReactMount.render(nextElement, container, callback) ___
  2. |=ReactMount._renderSubtreeIntoContainer() |
  3. |-ReactMount._renderNewRootComponent() |
  4. |-instantiateReactComponent() |
  5. |~batchedMountComponentIntoNode() upper half
  6. |~mountComponentIntoNode() (平台无关)
  7. |-ReactReconciler.mountComponent() |
  8. |-ReactCompositeComponent.mountComponent() |
  9. |-ReactCompositeComponent.performInitialMount() |
  10. |-instantiateReactComponent() _|_
  11. |-ReactDOMComponent.mountComponent() lower half
  12. |-_mountImageIntoNode() (HTML DOM 相关下一篇讲解)
  13. _|_
三、总结

本文讲解了 ReactCompositeComponent[T] 转化为 ReactDomComponent 的过程。代码一层一层嵌套递归执行,最终的目的是解析Virtual Dom对象,将其转换为 HTML。本文讲解的大部分代码是跟平台无关的,也就是 ReactDOM 和 ReactNative 共享的。生成 HTML 的任务是由 ReactDomComponent 负责的,将在下一篇文章讲解。

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

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

相关文章

  • React 源码深度解读(三):首次 DOM 元素渲染 - Part 3

    摘要:在学习源码的过程中,给我帮助最大的就是这个系列文章,于是决定基于这个系列文章谈一下自己的理解。到此为止,首次渲染就完成啦总结从启动到元素渲染到页面,并不像看起来这么简单,中间经历了复杂的层级调用。 前言 React 是一个十分庞大的库,由于要同时考虑 ReactDom 和 ReactNative ,还有服务器渲染等,导致其代码抽象化程度很高,嵌套层级非常深,阅读其源码是一个非常艰辛的过...

    U2FsdGVkX1x 评论0 收藏0
  • React 源码深度解读(一):首次DOM元素渲染 - Part 1

    摘要:调用栈是这样的这里生成的我们将其命名为,它将作为参数传入到。整个的调用栈是这样的组件间的层级结构是这样的到此为止,顶层对象已经构造完毕,下一步就是调用来自的方法,进行页面的渲染了。通过表达的结构最终会转化为一个纯对象,用于下一步的渲染。 欢迎关注我的公众号睿Talk,获取我最新的文章:showImg(https://segmentfault.com/img/bVbmYjo); 一、前言...

    daydream 评论0 收藏0
  • React 源码深度解读(四):首次自定义组件渲染 - Part 1

    摘要:本篇开始介绍自定义组件是如何渲染的。组件将自定义组件命名为,结构如下经过编译后,生成如下代码构建顶层包装组件跟普通元素渲染一样,第一步先会执行创建为的。调用顺序已在代码中注释。先看图,这部分内容将在下回分解 前言 React 是一个十分庞大的库,由于要同时考虑 ReactDom 和 ReactNative ,还有服务器渲染等,导致其代码抽象化程度很高,嵌套层级非常深,阅读其源码是一个非...

    Warren 评论0 收藏0
  • React 源码深度解读(五):首次自定义组件渲染 - Part 2

    摘要:的和真正有效的都各只有一行代码的调用栈如下这中间的函数调用逻辑很清晰,最终会走到这里这里的逻辑很简单,如果不是数组,则调用回调函数如果是数组,则继续调用自身,相当于深度优先遍历。这里的回调函数就是中的这里直接调用,创建。 前言 React 是一个十分庞大的库,由于要同时考虑 ReactDom 和 ReactNative ,还有服务器渲染等,导致其代码抽象化程度很高,嵌套层级非常深,阅读...

    william 评论0 收藏0
  • React 源码深度解读(八):事务 - Part 2

    摘要:前言是一个十分庞大的库,由于要同时考虑和,还有服务器渲染等,导致其代码抽象化程度很高,嵌套层级非常深,阅读其源码是一个非常艰辛的过程。在学习源码的过程中,给我帮助最大的就是这个系列文章,于是决定基于这个系列文章谈一下自己的理解。 前言 React 是一个十分庞大的库,由于要同时考虑 ReactDom 和 ReactNative ,还有服务器渲染等,导致其代码抽象化程度很高,嵌套层级非常...

    airborne007 评论0 收藏0

发表评论

0条评论

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