這篇文章帶給大家的內容是關於React首次渲染解析二(純DOM元素),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
上一篇文章中,介紹了頂層物件ReactCompositeComponent[T]是如何建構的,接下來我們來看看 batchedMountComponentIntoNode 做了什麼事情。
本文將要講解的呼叫堆疊是下面這個樣子的:
|=ReactMount.render(nextElement, container, callback) ___ |=ReactMount._renderSubtreeIntoContainer() | |-ReactMount._renderNewRootComponent() | |-instantiateReactComponent() | |~batchedMountComponentIntoNode() upper half |~mountComponentIntoNode() (平台无关) |-ReactReconciler.mountComponent() | |-ReactCompositeComponent.mountComponent() | |-ReactCompositeComponent.performInitialMount() | |-instantiateReactComponent() _|_ |-ReactDOMComponent.mountComponent() lower half |-_mountImageIntoNode() (HTML DOM 相关) _|_
如果看源碼,我們會留意到很多transaction相關的程式碼,我們暫時先將其忽略,會在後續的文章中進行講解。暫時可以理解為呼叫transaction.perform時,其實就是對第一個參數進行函數呼叫。跳過一些模版程式碼後,實際上做事情的是 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 用於建立 DOM 元素,而 ReactMount._mountImageIntoNode 則是將剛建立的 DOM 元素掛載到頁面。 ReactReconciler.mountComponent 會呼叫 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; }
現在各實例間的關係是這樣的:
#再繼續看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; },
當運行到var child = this._instantiateReactComponent
時,就會呼叫上篇文章說到的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 會在執行的時候,執行ReactDefaultInjection.inject()
將ReactDOMComponent 注入到ReactHostComponent 中,ReactHostComponent.createInternalComponent 最終會呼叫ReactDOdeed #我們將傳回的實例命名為ReactDOMComponent[ins]。
ReactReconciler.mountComponent 會呼叫 ReactDomComponent 的 mountComponent 方法,這就會牽涉到 HTML DOM 相關的內容,我們在下一篇文章會講解。
現在我們來看各實例間的關係:
#目前為止的呼叫堆疊:
// 文件位置: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; }
以上是React首次渲染解析二(純DOM元素)的詳細內容。更多資訊請關注PHP中文網其他相關文章!