国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題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關鍵字專題關鍵字專題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
當前位置: 首頁 - 科技 - 知識百科 - 正文

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

來源:懂視網 責編:小采 時間:2020-11-27 19:29:41
文檔

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

React首次渲染的解析一(純DOM元素):本篇文章給大家帶來的內容是關于React首次渲染的解析(純DOM元素),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。React 是一個十分龐大的庫,由于要同時考慮 ReactDom 和 ReactNative ,還有服務器渲染等,導致其代碼抽象化程度很高,嵌
推薦度:
導讀React首次渲染的解析一(純DOM元素):本篇文章給大家帶來的內容是關于React首次渲染的解析(純DOM元素),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。React 是一個十分龐大的庫,由于要同時考慮 ReactDom 和 ReactNative ,還有服務器渲染等,導致其代碼抽象化程度很高,嵌
本篇文章給大家帶來的內容是關于React首次渲染的解析(純DOM元素),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

React 是一個十分龐大的庫,由于要同時考慮 ReactDom 和 ReactNative ,還有服務器渲染等,導致其代碼抽象化程度很高,嵌套層級非常深,閱讀其源碼是一個非常艱辛的過程。在學習 React 源碼的過程中,給我幫助最大的就是這個系列文章,于是決定基于這個系列文章談一下自己的理解。本文會大量用到原文中的例子,想體會原汁原味的感覺,推薦閱讀原文。

本系列文章將基于 React 15.4.2。

  • React.createElement

  • 在寫 React 項目的時候,我們一般會直接用 JSX 的形式來寫,而 JSX 經過 Babel 編譯后最終會將 HTML 標簽轉換為React.createElement的函數形式。如果想進行更深入的了解,可以看我之前寫的這篇文章:你不知道的Virtual DOM(一):Virtual Dom介紹。文章中的h函數,如果不在 Babel 中配置的話,默認就是React.createElement。

    下面,我們將從一個最簡單的例子,來看React是如何渲染的

    ReactDOM.render(
     <h1 style={{"color":"blue"}}>hello world</h1>,
     document.getElementById('root')
    );

    經過JSX編譯后,會是下面這個樣子

    ReactDOM.render(
     React.createElement(
     'h1',
     { style: { "color": "blue" } },
     'hello world'
     ),
     document.getElementById('root')
    );

    先來看下React.createElement的源碼。

    // 文件位置:src/isomorphic/React.js
    
    var ReactElement = require('ReactElement');
    
    ...
    
    var createElement = ReactElement.createElement;
    
    ...
    
    var React = {
     ...
     
     createElement: createElement,
     
     ...
    }
    
    module.exports = React;

    最終的實現需要查看ReactElement.createElement

    // 文件位置:src/isomorphic/classic/element/ReactElement.js
    
    ReactElement.createElement = function (type, config, children) {
     ...
    
     // 1. 將過濾后的有效的屬性,從config拷貝到props
     if (config != null) {
     
     ...
     
     for (propName in config) {
     if (hasOwnProperty.call(config, propName) &&
     !RESERVED_PROPS.hasOwnProperty(propName)) {
     props[propName] = config[propName];
     }
     }
     }
    
     // 2. 將children以數組的形式拷貝到props.children屬性
     var childrenLength = arguments.length - 2;
     if (childrenLength === 1) {
     props.children = children;
     } else if (childrenLength > 1) {
     var childArray = Array(childrenLength);
     for (var i = 0; i < childrenLength; i++) {
     childArray[i] = arguments[i + 2];
     }
     if (__DEV__) {
     if (Object.freeze) {
     Object.freeze(childArray);
     }
     }
     props.children = childArray;
     }
    
     // 3. 默認屬性賦值
     if (type && type.defaultProps) {
     var defaultProps = type.defaultProps;
     for (propName in defaultProps) {
     if (props[propName] === undefined) {
     props[propName] = defaultProps[propName];
     }
     }
     }
     
     ...
     
     return ReactElement(
     type,
     key,
     ref,
     self,
     source,
     ReactCurrentOwner.current,
     props
     );
    };

    本質上只做了3件事:

    1. 將過濾后的有效的屬性,從config拷貝到props

    2. 將children以數組的形式拷貝到props.children屬性

    3. 默認屬性賦值

    最終的返回值是ReactElement。我們再來看看它做了什么

    // 文件位置:src/isomorphic/classic/element/ReactElement.js
    
    var ReactElement = function (type, key, ref, self, source, owner, props) {
     var element = {
     // This tag allow us to uniquely identify this as a React Element
     $$typeof: REACT_ELEMENT_TYPE,
    
     // Built-in properties that belong on the element
     type: type,
     key: key,
     ref: ref,
     props: props,
    
     // Record the component responsible for creating this element.
     _owner: owner,
     };
     
     ...
    
     return element;
    };

    最終只是返回了一個簡單對象。調用棧是這樣的:

    React.createElement
    |=ReactElement.createElement(type, config, children)
     |-ReactElement(type,..., props)

    這里生成的 ReactElement 我們將其命名為ReactElement[1],它將作為參數傳入到 ReactDom.render。

  • ReactDom.render

  • ReactDom.render 最終會調用 ReactMount 的 _renderSubtreeIntoContainer:

    // 文件位置:src/renderers/dom/client/ReactMount.js
    
    _renderSubtreeIntoContainer: function (parentComponent, nextElement, container, callback) {
     ...
     var nextWrappedElement = React.createElement(
     TopLevelWrapper, 
     {
     child: nextElement
     }
     );
    
     ...
     
     var component = ReactMount._renderNewRootComponent(
     nextWrappedElement,
     container,
     shouldReuseMarkup,
     nextContext
     )._renderedComponent.getPublicInstance();
     
     ...
     
     return component;
    },
    
    ...
    
    var TopLevelWrapper = function () {
     this.rootID = topLevelRootCounter++;
    };
    
    TopLevelWrapper.prototype.isReactComponent = {};
    
    TopLevelWrapper.prototype.render = function () {
     return this.props.child;
    };
    
    TopLevelWrapper.isReactTopLevelWrapper = true;
    
    ...
    
    _renderNewRootComponent: function (
     nextElement,
     container,
     shouldReuseMarkup,
     context
    ) {
     ...
     
     var componentInstance = instantiateReactComponent(nextElement, false);
    
     ...
    
     return componentInstance;
    },

    這里又會調用到另一個文件 instantiateReactComponent:

    // 文件位置:src/renders/shared/stack/reconciler/instantiateReactComponent.js
    
    function instantiateReactComponent(node, shouldHaveDebugID) {
     var instance;
    
     ...
    
     instance = new ReactCompositeComponentWrapper(element);
     
     ...
    
     return instance;
    }
    
    // To avoid a cyclic dependency, we create the final class in this module
    var ReactCompositeComponentWrapper = function (element) {
     this.construct(element);
    };
    
    Object.assign(
     ReactCompositeComponentWrapper.prototype,
     ReactCompositeComponent, 
     {
     _instantiateReactComponent: instantiateReactComponent,
     }
    );

    這里又會調用到另一個文件 ReactCompositeComponent:

    // 文件位置:src/renders/shared/stack/reconciler/ReactCompositeComponent.js
    
    var ReactCompositeComponent = {
     construct: function (element) {
     this._currentElement = element;
     this._rootNodeID = 0;
     this._compositeType = null;
     this._instance = null;
     this._hostParent = null;
     this._hostContainerInfo = null;
    
     // See ReactUpdateQueue
     this._updateBatchNumber = null;
     this._pendingElement = null;
     this._pendingStateQueue = null;
     this._pendingReplaceState = false;
     this._pendingForceUpdate = false;
    
     this._renderedNodeType = null;
     this._renderedComponent = null;
     this._context = null;
     this._mountOrder = 0;
     this._topLevelWrapper = null;
    
     // See ReactUpdates and ReactUpdateQueue.
     this._pendingCallbacks = null;
    
     // ComponentWillUnmount shall only be called once
     this._calledComponentWillUnmount = false;
    
     if (__DEV__) {
     this._warnedAboutRefsInRender = false;
     }
     }
     
     ...
    }

    我們用ReactCompositeComponent[T]來表示這里生成的頂層 component。

    整個的調用棧是這樣的:

    ReactDOM.render
    |=ReactMount.render(nextElement, container, callback)
    |=ReactMount._renderSubtreeIntoContainer()
     |-ReactMount._renderNewRootComponent(
     nextWrappedElement, // scr:------------------> ReactElement[2]
     container, // scr:------------------> document.getElementById('root')
     shouldReuseMarkup, // scr: null from ReactDom.render()
     nextContext, // scr: emptyObject from ReactDom.render()
     )
     |-instantiateReactComponent(
     node, // scr:------------------> ReactElement[2]
     shouldHaveDebugID /* false */
     )
     |-ReactCompositeComponentWrapper(
     element // scr:------------------> ReactElement[2]
     );
     |=ReactCompositeComponent.construct(element)

    組件間的層級結構是這樣的:

    4100563483-5bc9a047e64c5_articlex.png

    當頂層組件構建完畢后,下一步就是調用 batchedMountComponentIntoNode(來自 ReactMount 的 _renderNewRootComponent方法),進行頁面的渲染了。

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

    文檔

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

    React首次渲染的解析一(純DOM元素):本篇文章給大家帶來的內容是關于React首次渲染的解析(純DOM元素),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。React 是一個十分龐大的庫,由于要同時考慮 ReactDom 和 ReactNative ,還有服務器渲染等,導致其代碼抽象化程度很高,嵌
    推薦度:
    標簽: 講解 首次 解析
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 亚洲乱码中文论理电影 | 亚洲欧美综合图片 | www国产精品 | 日本免费一区二区三区 | 亚洲首页在线观看 | 精品72久久久久久久中文字幕 | 精品国产成人综合久久小说 | 自拍偷拍 欧美日韩 | 久久精品无码一区二区日韩av | 成人国产一区二区 | www.日韩在线 | 国产日韩欧美视频 | 亚洲一级二级 | 国产日韩欧美精品一区二区三区 | 国产高清视频免费在线观看 | 日韩成人免费在线 | 亚洲人成网站999久久久综合 | 欧洲亚洲一区 | 国产又大又粗又猛又爽的视频 | 国产精品视频一区二区噜噜 | 日韩欧美综合 | 91精品国产91久久久久久 | 影音先锋中文字幕资源 | a天堂专区一区二区三区 | 亚洲va欧美ⅴa国产va影院 | 午夜欧美在线 | 国产人成精品综合欧美成人 | 全免费午夜一级毛片一级毛 | 日本a黄 | 日本不卡一区二区三区 最新 | 亚洲一区中文字幕在线 | 欧美日韩精品一区二区三区视频在线 | 一道本一区二区三区 | 亚洲欧美网址 | 久久国产精品视频一区 | 亚洲 欧美 日韩 另类 | 欧美精品第1页在线播放 | 日韩高清欧美 | 日韩国产在线 | 欧美亚洲天堂 | 曰韩欧美|