欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

React首次渲染的示例分析

小編給大家分享一下React首次渲染的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

“只有客戶發(fā)展了,才有我們的生存與發(fā)展!”這是成都創(chuàng)新互聯(lián)的服務(wù)宗旨!把網(wǎng)站當(dāng)作互聯(lián)網(wǎng)產(chǎn)品,產(chǎn)品思維更注重全局思維、需求分析和迭代思維,在網(wǎng)站建設(shè)中就是為了建設(shè)一個(gè)不僅審美在線,而且實(shí)用性極高的網(wǎng)站。創(chuàng)新互聯(lián)對(duì)做網(wǎng)站、網(wǎng)站制作、網(wǎng)站制作、網(wǎng)站開發(fā)、網(wǎng)頁設(shè)計(jì)、網(wǎng)站優(yōu)化、網(wǎng)絡(luò)推廣、探索永無止境。

  • React.createElement

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

下面,我們將從一個(gè)最簡(jiǎn)單的例子,來看React是如何渲染的

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

經(jīng)過JSX編譯后,會(huì)是下面這個(gè)樣子

ReactDOM.render(
    React.createElement(
        'h2',
        { 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;

最終的實(shí)現(xiàn)需要查看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以數(shù)組的形式拷貝到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. 默認(rèn)屬性賦值
    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
    );
};

本質(zhì)上只做了3件事:

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

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

  3. 默認(rèn)屬性賦值

最終的返回值是ReactElement。我們?cè)賮砜纯此隽耸裁?/p>

// 文件位置: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;
};

最終只是返回了一個(gè)簡(jiǎn)單對(duì)象。調(diào)用棧是這樣的:

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

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

  • ReactDom.render

ReactDom.render 最終會(huì)調(diào)用 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;
},

這里又會(huì)調(diào)用到另一個(gè)文件 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,
    }
);

這里又會(huì)調(diào)用到另一個(gè)文件 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。

整個(gè)的調(diào)用棧是這樣的:

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)

組件間的層級(jí)結(jié)構(gòu)是這樣的:

React首次渲染的示例分析

當(dāng)頂層組件構(gòu)建完畢后,下一步就是調(diào)用 batchedMountComponentIntoNode(來自 ReactMount 的 _renderNewRootComponent方法),進(jìn)行頁面的渲染了。

以上是“React首次渲染的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

名稱欄目:React首次渲染的示例分析
網(wǎng)頁URL:http://chinadenli.net/article14/ppchge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站收錄、自適應(yīng)網(wǎng)站、虛擬主機(jī)、網(wǎng)站設(shè)計(jì)公司、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)
国产又黄又猛又粗又爽的片| 欧美高潮喷吹一区二区| 麻豆视传媒短视频在线看| 午夜色午夜视频之日本| 91亚洲国产成人久久| 中文字幕在线区中文色 | 欧美性欧美一区二区三区| 91插插插外国一区二区| 五月天婷亚洲天婷综合网| 日本黄色高清视频久久| 欧美黄色成人真人视频| 国产成人在线一区二区三区| 国产肥妇一区二区熟女精品| 极品少妇一区二区三区精品视频| 日本在线视频播放91| 日本一品道在线免费观看| 久久热中文字幕在线视频| 亚洲一区二区精品免费视频| 日韩av欧美中文字幕| 亚洲国产精品一区二区毛片| 国产爆操白丝美女在线观看| 微拍一区二区三区福利| 少妇人妻一级片一区二区三区| 亚洲国产一区精品一区二区三区色| 亚洲中文字幕剧情在线播放| 69老司机精品视频在线观看| 一区二区三区人妻在线| 日韩免费av一区二区三区| 日本一区二区三区黄色| 欧美国产日产综合精品| 久久这里只精品免费福利| 亚洲国产精品久久综合网| 国产日韩在线一二三区| 国产女同精品一区二区| 国产欧美高清精品一区| 中文久久乱码一区二区| 精品日韩中文字幕视频在线| 精产国品一二三区麻豆| 不卡一区二区在线视频| 婷婷九月在线中文字幕| 亚洲天堂国产精品久久精品|