资讯专栏INFORMATION COLUMN

React 源码深度解读(五):首次自定义组件渲染 - Part 2

william / 969人阅读

摘要:的和真正有效的都各只有一行代码的调用栈如下这中间的函数调用逻辑很清晰,最终会走到这里这里的逻辑很简单,如果不是数组,则调用回调函数如果是数组,则继续调用自身,相当于深度优先遍历。这里的回调函数就是中的这里直接调用,创建。

前言

React 是一个十分庞大的库,由于要同时考虑 ReactDom 和 ReactNative ,还有服务器渲染等,导致其代码抽象化程度很高,嵌套层级非常深,阅读其源码是一个非常艰辛的过程。在学习 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[ins]被初始化后,App[ins]的 render 方法被调用,生成 ReactElement 树,然后对应的ReactDOMComponent[6]被返回。下面我们来看看这个ReactDOMComponent[6]是如何转化为 DOM 树的。

</>复制代码

  1. performInitialMount: function (renderedElement, hostParent,
  2. hostContainerInfo, transaction, context) {
  3. ...
  4. // 这里会调用 App 实例的 render 方法,而 render 的返回值是 React.createElement 的嵌套调用。
  5. if (renderedElement === undefined) {
  6. renderedElement = this._renderValidatedComponent();
  7. }
  8. ...
  9. // 上回讲到这里
  10. // 返回 ReactDOMComponent[6]
  11. var child = this._instantiateReactComponent(
  12. renderedElement,
  13. nodeType !== ReactNodeTypes.EMPTY /* shouldHaveDebugID */
  14. );
  15. this._renderedComponent = child;
  16. // 今天讲这部分
  17. var markup = ReactReconciler.mountComponent(
  18. child,
  19. transaction,
  20. hostParent,
  21. hostContainerInfo,
  22. this._processChildContext(context),
  23. debugID
  24. );
  25. return markup;
  26. },

ReactDOMComponent[6].mountComponent

ReactReconciler.mountComponent 会触发ReactDOMComponent[6]的 mountComponent 方法,调用栈如下:

</>复制代码

  1. ...
  2. |~mountComponentIntoNode() |
  3. |-ReactReconciler.mountComponent() |
  4. |-ReactCompositeComponent[T].mountComponent() |
  5. |-ReactCompositeComponent[T].performInitialMount() upper half
  6. |-ReactReconciler.mountComponent() |
  7. |-ReactCompositeComponent[ins].mountComponent() |
  8. |-this.performInitialMount() |
  9. |-this._renderValidatedComponent() |
  10. |-instantiateReactComponent() _|_
  11. (we are here) |
  12. |-ReactDOMComponent[6].mountComponent( |
  13. transaction, // scr: -----> not of interest |
  14. hostParent, // scr: -----> null |
  15. hostContainerInfo,// scr:---------------------> ReactDOMContainerInfo[ins] lower half
  16. context // scr: -----> not of interest |
  17. ) |
  18. ...

</>复制代码

  1. mountComponent: function (
  2. transaction,
  3. hostParent,
  4. hostContainerInfo,
  5. context
  6. ) {
  7. ...
  8. var mountImage;
  9. if (transaction.useCreateElement) {
  10. var ownerDocument = hostContainerInfo._ownerDocument;
  11. ...
  12. // 创建 div 元素
  13. el = ownerDocument.createElement(this._currentElement.type);
  14. ...
  15. // 设置 attributes
  16. if (!this._hostParent) {
  17. DOMPropertyOperations.setAttributeForRoot(el);
  18. }
  19. // 设置 properties
  20. this._updateDOMProperties(null, props, transaction);
  21. // 构造 DOM 树
  22. var lazyTree = DOMLazyTree(el);
  23. // 遍历子节点并创建 DOM 结点
  24. this._createInitialChildren(transaction, props, context, lazyTree);
  25. mountImage = lazyTree;
  26. }
  27. ...
  28. return mountImage;
  29. }

这里主要做的事情有3部分:

创建 DOM 元素

设置 attributes 和 properties

遍历子元素并重复上述过程

这时候的数据结构如下:

流程图:

_createInitialChildren 遍历子节点并创建 DOM 结点

下面来看一下 _createInitialChildren 的细节:

</>复制代码

  1. _createInitialChildren: function (transaction, props, context, lazyTree) {
  2. // Intentional use of != to avoid catching zero/false.
  3. var innerHTML = props.dangerouslySetInnerHTML;
  4. if (innerHTML != null) {
  5. if (innerHTML.__html != null) {
  6. DOMLazyTree.queueHTML(lazyTree, innerHTML.__html);
  7. }
  8. } else {
  9. // 如果是 string 或者 number,返回 true
  10. var contentToUse =
  11. CONTENT_TYPES[typeof props.children] ? props.children :
  12. null;
  13. var childrenToUse = contentToUse != null ? null : props.children;
  14. // 直接渲染字符串
  15. if (contentToUse != null) {
  16. // Avoid setting textContent when the text is empty. In IE11 setting
  17. // textContent on a text area will cause the placeholder to not
  18. // show within the textarea until it has been focused and blurred again.
  19. // https://github.com/facebook/react/issues/6731#issuecomment-254874553
  20. if (contentToUse !== "") {
  21. DOMLazyTree.queueText(lazyTree, contentToUse);
  22. }
  23. }
  24. // 渲染子节点
  25. else if (childrenToUse != null) {
  26. var mountImages = this.mountChildren(
  27. childrenToUse,
  28. transaction,
  29. context
  30. );
  31. for (var i = 0; i < mountImages.length; i++) {
  32. DOMLazyTree.queueChild(lazyTree, mountImages[i]);
  33. }
  34. }
  35. }
  36. },

这部分代码十分好懂,就 3 条分支:

设置了 dangerouslySetInnerHTML 属性,直接渲染 HTML

子节点类型为 string 或 number,渲染字符

其它情况就需要将 ReactElement 转换成 ReactDOMComponent 或 ReactCompositeComponent 作进一步的渲染。

DOMLazyTree 的 queueText 和 queueChild 真正有效的都各只有一行代码:

</>复制代码

  1. function queueText(tree, text) {
  2. if (enableLazy) { // scr: NO, I mean, false
  3. ...
  4. } else {
  5. setTextContent(tree.node, text);
  6. }
  7. }
  8. var setTextContent = function (node, text) {
  9. if (text) {
  10. var firstChild = node.firstChild;
  11. if (firstChild && firstChild === node.lastChild && firstChild.nodeType === 3) { // scr: false
  12. ...
  13. }
  14. }
  15. node.textContent = text; // scr: the only effective line
  16. };
  17. function queueChild(parentTree, childTree) {
  18. if (enableLazy) { // scr: again, false
  19. ...
  20. } else {
  21. parentTree.node.appendChild(childTree.node);
  22. }
  23. }

mountChildren 的调用栈如下:

</>复制代码

  1. ReactDOMComponent[6].mountComponent() <-------------------------|
  2. (we are here) |
  3. |-this._createInitialChildren() |
  4. ?{1} |
  5. |-DOMLazyTree.queueText() |
  6. ?{2} |
  7. |-this.mountChildren() // scr: ---------------> 1)(a) |
  8. |-this._reconcilerInstantiateChildren() |
  9. |-ReactChildReconciler.instantiateChildren() |
  10. |-traverseAllChildren() |
  11. |-traverseAllChildrenImpl() <------|inner |
  12. |↻traverseAllChildrenImpl() ------|recursion |
  13. |-instantiateChild() |
  14. |-instantiateReactComponent() |
  15. |↻ReactDOMComponent.mountComponent() // scr: -> 1)(b)---|
  16. |↻DOMLazyTree.queueChild() // scr: ---------------> 2)

这中间的函数调用逻辑很清晰,最终会走到 traverseAllChildrenImpl 这里:

</>复制代码

  1. function traverseAllChildrenImpl(
  2. children,
  3. nameSoFar,
  4. callback,
  5. traverseContext
  6. ) {
  7. var type = typeof children;
  8. if (type === "undefined" || type === "boolean") {
  9. // All of the above are perceived as null.
  10. children = null;
  11. }
  12. if (children === null ||
  13. type === "string" ||
  14. type === "number" ||
  15. // The following is inlined from ReactElement. This means we can optimize
  16. // some checks. React Fiber also inlines this logic for similar purposes.
  17. (type === "object" && children.$$typeof === REACT_ELEMENT_TYPE)) {
  18. callback(
  19. traverseContext,
  20. children,
  21. // If it"s the only child, treat the name as if it was wrapped in an array
  22. // so that it"s consistent if the number of children grows.
  23. nameSoFar === "" ? SEPARATOR + getComponentKey(children, 0) :
  24. nameSoFar
  25. );
  26. return 1;
  27. }
  28. var child;
  29. var nextName;
  30. var subtreeCount = 0; // Count of children found in the current subtree.
  31. var nextNamePrefix = nameSoFar === "" ? SEPARATOR : nameSoFar +
  32. SUBSEPARATOR;
  33. if (Array.isArray(children)) {
  34. for (var i = 0; i < children.length; i++) {
  35. child = children[i];
  36. nextName = nextNamePrefix + getComponentKey(child, i);
  37. subtreeCount += traverseAllChildrenImpl(
  38. child,
  39. nextName,
  40. callback,
  41. traverseContext
  42. );
  43. }
  44. } else {
  45. ...
  46. }
  47. return subtreeCount;
  48. }

这里的逻辑很简单,如果 children 不是数组,则调用回调函数;如果是数组,则继续调用自身,相当于深度优先遍历。这里的回调函数就是 ReactChildReconciler 中的 instantiateChild:

</>复制代码

  1. function instantiateChild(childInstances, child, name, selfDebugID) {
  2. ...
  3. if (child != null && keyUnique) {
  4. childInstances[name] = instantiateReactComponent(child, true);
  5. }
  6. }

这里直接调用 instantiateReactComponent,创建ReactDOMComponent。所有的ReactDOMComponent的创建顺序如下:

</>复制代码

  1. ReactDOMComponent[6].mountComponent()
  2. |-this._createInitialChildren()
  3. |-this.mountChildren()
  4. ... |↻instantiateReactComponent()[4,5]
  5. |-ReactDOMComponent[5].mountComponent()
  6. |-this._createInitialChildren()
  7. |-node.textContent = text; // scr: [5] done
  8. |-ReactDOMComponent[4].mountComponent()
  9. |-this._createInitialChildren()
  10. |-this.mountChildren()
  11. ... |↻instantiateReactComponent()[2,3]
  12. |-ReactDOMComponent[2].mountComponent() // scr: [2] done
  13. |-ReactDOMComponent[3].mountComponent()
  14. |-this._createInitialChildren()
  15. |-node.textContent = text; // scr: [3] done
  16. |↻node[4].appendChild()[2,3] // scr: [4] done
  17. |↻node[6].appendChild()[4,5] // scr: [6] done

完成的流程图:

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

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

相关文章

  • React 源码深度解读(四):次自定义组件渲染 - Part 1

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

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

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

    U2FsdGVkX1x 评论0 收藏0
  • React 源码深度解读(六):依赖注入

    摘要:依赖注入和控制反转,这两个词经常一起出现。一句话表述他们之间的关系依赖注入是控制反转的一种实现方式。而两者有大量的代码都是可以共享的,这就是依赖注入的使用场景了。下一步就是创建具体的依赖内容,然后注入到需要的地方这里的等于这个对象。 前言 React 是一个十分庞大的库,由于要同时考虑 ReactDom 和 ReactNative ,还有服务器渲染等,导致其代码抽象化程度很高,嵌套层级...

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

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

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

    摘要:本文将要讲解的调用栈是下面这个样子的平台无关相关如果看源码,我们会留意到很多相关的代码,我们暂时先将其忽略,会在后续的文章中进行讲解。现在我们来看一下各实例间的关系目前为止的调用栈平台无关相关下一篇讲解三总结本文讲解了转化为的过程。 欢迎关注我的公众号睿Talk,获取我最新的文章:showImg(https://segmentfault.com/img/bVbmYjo); 一、前言 R...

    wean 评论0 收藏0

发表评论

0条评论

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