<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

        React首次渲染解析二(純DOM元素)

        來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 19:29:41
        文檔

        React首次渲染解析二(純DOM元素)

        React首次渲染解析二(純DOM元素):本篇文章給大家?guī)淼膬?nèi)容是關(guān)于React首次渲染解析二(純DOM元素),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。上一篇文章中,介紹了頂層對象ReactCompositeComponent[T]是如何構(gòu)造的,接下來我們看看 batchedMountCom
        推薦度:
        導(dǎo)讀React首次渲染解析二(純DOM元素):本篇文章給大家?guī)淼膬?nèi)容是關(guān)于React首次渲染解析二(純DOM元素),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。上一篇文章中,介紹了頂層對象ReactCompositeComponent[T]是如何構(gòu)造的,接下來我們看看 batchedMountCom
        本篇文章給大家?guī)淼膬?nèi)容是關(guān)于React首次渲染解析二(純DOM元素),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

        上一篇文章中,介紹了頂層對象ReactCompositeComponent[T]是如何構(gòu)造的,接下來我們看看 batchedMountComponentIntoNode 做了什么事情。

        本文將要講解的調(diào)用棧是下面這個樣子的:

        |=ReactMount.render(nextElement, container, callback) ___
        |=ReactMount._renderSubtreeIntoContainer() |
         |-ReactMount._renderNewRootComponent() |
         |-instantiateReactComponent() |
         |~batchedMountComponentIntoNode() upper half
         |~mountComponentIntoNode() (平臺無關(guān))
         |-ReactReconciler.mountComponent() |
         |-ReactCompositeComponent.mountComponent() |
         |-ReactCompositeComponent.performInitialMount() |
         |-instantiateReactComponent() _|_
         |-ReactDOMComponent.mountComponent() lower half
         |-_mountImageIntoNode() (HTML DOM 相關(guān))
         _|_

        如果看源碼,我們會留意到很多transaction相關(guān)的代碼,我們暫時先將其忽略,會在后續(xù)的文章中進行講解。暫時可以理解為調(diào)用transaction.perform時,實際上就是對第一個參數(shù)進行函數(shù)調(diào)用。跳過一些模版代碼后,實際上做事情的是 mountComponentIntoNode 這個方法。

        // 文件位置:src/renderers/dom/client/ReactMount.js
        
        function mountComponentIntoNode(
         wrapperInstance, // ReactCompositeComponent[T]
         container, // document.getElementById("root")
         transaction,
         shouldReuseMarkup,
         context
        ) {
         ...
         
         var markup = ReactReconciler.mountComponent(
         wrapperInstance,
         transaction,
         null,
         ReactDOMContainerInfo(wrapperInstance, container),
         context,
         0 /* parentDebugID */
         );
        
         ...
         
         ReactMount._mountImageIntoNode(
         markup,
         container,
         wrapperInstance,
         shouldReuseMarkup,
         transaction
         );
        }

        ReactReconciler.mountComponent 用于創(chuàng)建 DOM 元素,而 ReactMount._mountImageIntoNode 則是將剛創(chuàng)建的 DOM 元素掛載到頁面。ReactReconciler.mountComponent 會調(diào)用 ReactCompositeComponent[T]的 mountComponent 方法。在看 mountComponent 方法前,還需要先準備好 hostContainerInfo,由 ReactDOMContainerInfo 生成:

        // 文件位置:src/renderers/shared/stack/reconciler/ReactContainerInfo.js
        
        function ReactDOMContainerInfo(
         topLevelWrapper, // ReactCompositeComponent[T]
         node // document.getElementById("root")
        ) {
         var info = {
         _topLevelWrapper: topLevelWrapper,
         _idCounter: 1,
         _ownerDocument: node ?
         node.nodeType === DOC_NODE_TYPE ? node : node.ownerDocument : null,
         _node: node,
         _tag: node ? node.nodeName.toLowerCase() : null,
         _namespaceURI: node ? node.namespaceURI : null,
         };
         
         ...
         
         return info;
        }

        現(xiàn)在各實例間的關(guān)系是這樣的:

        3778215164-5bc9e101a4f2e_articlex.png

        再繼續(xù)看 mountComponent 方法:

        // 文件位置:src/renderers/shared/stack/reconciler/ReactCompositeComponent.js
        
        mountComponent: function (
         transaction,
         hostParent,
         hostContainerInfo,
         context
        ) {
         ...
        
         // this._currentElement 為ReactElement[2](TopLevelWrapper)
         var publicProps = this._currentElement.props;
         var publicContext = this._processContext(context);
        
         // TopLevelWrapper
         var Component = this._currentElement.type;
        
         ...
        
         // Initialize the public class
         var doConstruct = shouldConstruct(Component);
         
         // 生成TopLevelWrapper 實例
         var inst = this._constructComponent(
         doConstruct,
         publicProps,
         publicContext,
         updateQueue
         );
         
         ...
        
         var markup;
         
         ...
         
         markup = this.performInitialMount(renderedElement,
         hostParent, hostContainerInfo, transaction, context
        
         ...
        
         return markup;
        },
        
        performInitialMount: function (renderedElement, hostParent,
         hostContainerInfo, transaction, context) {
         
         // TopLevelWrapper 實例
         var inst = this._instance;
        
         ...
         
         // If not a stateless component, we now render
         if (renderedElement === undefined) {
         // 返回值為 ReactElement[1]
         renderedElement = this._renderValidatedComponent();
         }
        
         // 返回 ReactNodeTypes.HOST
         var nodeType = ReactNodeTypes.getType(renderedElement);
         
         this._renderedNodeType = nodeType;
         
         // instantiateReactComponent.js
         var child = this._instantiateReactComponent(
         renderedElement,
         nodeType !== ReactNodeTypes.EMPTY /* shouldHaveDebugID */
         );
         this._renderedComponent = child;
        
         var markup = ReactReconciler.mountComponent(
         child,
         transaction,
         hostParent,
         hostContainerInfo,
         this._processChildContext(context),
         debugID
         );
        
         ...
        
         return markup;
        },

        當(dāng)運行到var child = this._instantiateReactComponent時,就會調(diào)用上篇文章說到的instantiateReactComponent文件:

        // 文件位置:src/renderers/shared/stack/reconciler/instantiateReactComponent.js
        
        function instantiateReactComponent(node, shouldHaveDebugID) {
         var instance;
        
         ...
         
         } else if (typeof node === 'object') {
         ...
        
         // element.type 為 ‘h1’
         if (typeof element.type === 'string') {
         instance = ReactHostComponent.createInternalComponent(element);
         } 
        
         return instance;
        }

        ReactDom 會在執(zhí)行的時候,執(zhí)行ReactDefaultInjection.inject()將 ReactDOMComponent 注入到 ReactHostComponent 中,ReactHostComponent.createInternalComponent 最終會調(diào)用 ReactDOMComponent:

        // 文件位置:src/renderers/dom/shared/ReactDomComponent.js
        
        function ReactDOMComponent(element) {
         // h1
         var tag = element.type;
         
         validateDangerousTag(tag);
         
         // ReactElement[1]
         this._currentElement = element;
         
         this._tag = tag.toLowerCase();
         this._namespaceURI = null;
         this._renderedChildren = null;
         this._previousStyle = null;
         this._previousStyleCopy = null;
         this._hostNode = null;
         this._hostParent = null;
         this._rootNodeID = 0;
         this._domID = 0;
         this._hostContainerInfo = null;
         this._wrapperState = null;
         this._topLevelWrapper = null;
         this._flags = 0;
        }

        我們將返回的實例命名為 ReactDOMComponent[ins]。

        ReactReconciler.mountComponent 會調(diào)用 ReactDomComponent 的 mountComponent 方法,這就會涉及到 HTML DOM 相關(guān)的內(nèi)容,我們在下一篇進行講解。

        現(xiàn)在我們來看一下各實例間的關(guān)系:

        773279841-5bcaa4696ab21_articlex.png

        目前為止的調(diào)用棧:

        |=ReactMount.render(nextElement, container, callback) ___
        |=ReactMount._renderSubtreeIntoContainer() |
         |-ReactMount._renderNewRootComponent() |
         |-instantiateReactComponent() |
         |~batchedMountComponentIntoNode() upper half
         |~mountComponentIntoNode() (平臺無關(guān))
         |-ReactReconciler.mountComponent() |
         |-ReactCompositeComponent.mountComponent() |
         |-ReactCompositeComponent.performInitialMount() |
         |-instantiateReactComponent() _|_
         |-ReactDOMComponent.mountComponent() lower half
         |-_mountImageIntoNode() (HTML DOM 相關(guān)下一篇講解)
         _|_

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        React首次渲染解析二(純DOM元素)

        React首次渲染解析二(純DOM元素):本篇文章給大家?guī)淼膬?nèi)容是關(guān)于React首次渲染解析二(純DOM元素),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。上一篇文章中,介紹了頂層對象ReactCompositeComponent[T]是如何構(gòu)造的,接下來我們看看 batchedMountCom
        推薦度:
        標簽: 元素 首次 解析
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 国产自产拍精品视频免费看| 在线看无码的免费网站| 在线免费观看一区二区三区| 亚洲女人影院想要爱| 黄网站色在线视频免费观看| 亚洲高清免费在线观看| 久久久久免费看成人影片| 久久精品亚洲日本佐佐木明希| 日本高清免费观看| 亚洲AV美女一区二区三区| 99re热精品视频国产免费| 精品亚洲AV无码一区二区三区| 九九精品免费视频| AV激情亚洲男人的天堂国语| 亚洲国产天堂久久综合| 99在线免费观看| 亚洲综合免费视频| 最近中文字幕无免费视频| 国产亚洲Av综合人人澡精品| 亚洲精品视频免费观看| 免费h视频在线观看| 亚洲国产情侣一区二区三区| 啦啦啦手机完整免费高清观看| 婷婷国产偷v国产偷v亚洲| 国产亚洲精品看片在线观看| 久久免费区一区二区三波多野| 亚洲人成在线中文字幕| 国产无遮挡吃胸膜奶免费看 | av网站免费线看| 国产AV无码专区亚洲AV男同| 在线观看免费视频资源| 亚洲AV无码成人网站在线观看| 亚洲第一区精品日韩在线播放| 免费无码成人AV在线播放不卡| 亚洲精品国产精品| 亚洲av永久无码精品古装片| 最新中文字幕免费视频| 一本久久A久久免费精品不卡| 亚洲高清不卡视频| 亚洲成a人一区二区三区| 久久久久久国产精品免费无码|