<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无码不卡久久| 亚洲毛片在线免费观看| 亚洲精品综合久久中文字幕 | 99爱免费观看视频在线| 亚洲成色WWW久久网站| 久久精品乱子伦免费| 亚洲春色另类小说| 97无码免费人妻超级碰碰夜夜| 亚洲自偷自偷在线成人网站传媒| 日本免费中文字幕在线看| 瑟瑟网站免费网站入口| 亚洲精品高清无码视频| 最近中文字幕mv免费高清视频8 | 日韩精品电影一区亚洲| 国产精品综合专区中文字幕免费播放| 三上悠亚亚洲一区高清| 日韩精品无码免费一区二区三区| 亚洲天堂福利视频| 国产男女猛烈无遮挡免费视频| 一区二区三区在线观看免费 | 亚洲午夜电影一区二区三区| 浮力影院第一页小视频国产在线观看免费 | 日韩中文无码有码免费视频| 免费手机在线看片| 亚洲男人天堂2017| 浮力影院第一页小视频国产在线观看免费| 婷婷亚洲综合五月天小说在线| 国产亚洲精品影视在线产品 | 国产午夜亚洲精品不卡电影| 亚洲国产日韩在线视频| 国产一卡2卡3卡4卡2021免费观看| 亚洲国产成人久久精品大牛影视| 国产亚洲精品精品国产亚洲综合| 1区2区3区产品乱码免费| 亚洲av无码av在线播放| 亚洲AV无码成人精品区天堂 | 国产真人无遮挡作爱免费视频 | 日韩免费a级在线观看| 日韩精品无码免费一区二区三区|