日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

ReactElement源碼筆記

 路人甲Java 2021-08-21

ReactElement 源碼筆記

ReactElement通過 createElement創(chuàng)建,調(diào)用該方法需要 傳入三個(gè)參數(shù):

  • type
  • config
  • children

type指代這個(gè)ReactElement 的類型

config指代這個(gè)ReactElement 的屬性對象

children指代這個(gè)ReactElement 的子元素節(jié)點(diǎn)

  • 字符串比如 'div','p'代表原生DOM,稱為HostComponent
  • Class 類型是我們繼承自 Component 或者 PureComponent的組件,稱為ClassComponent
  • 方法就是 function Component
  • 原生提供的 Fragment、AsyncMode 等是 Symbol,會被特殊處理
  • TODO: 是否有其他的
//源碼位置: packages/react/src/ReactElement.js   
// createElement函數(shù)
export function createElement(type, config, children){
    // 一、處理config
    
    // 1. 判斷config是否為空
    
    // 2. 提取保留屬性(key, ref, self, soure)
    
    // 3. 其余屬性添加到新的props對象中
        for(propName in config){
            if(
                hasOwnProperty.call(config, propName)&&
                !RESERVED_PROPS.hasOwnProperty(propName)
            ){
                props[propName] = config[propName]
            }
        }
    
    // 二、處理children => props.children
    // (children可以超過一個(gè)),處理過后的children 被放入 props.children
        const childrenLength = arguments.length - 2;
        if (childrenLength === 1) {
            props.children = children;
        } else if (childrenLength > 1) {
            const childArray = Array(childrenLength);
            for (let i = 0; i < childrenLength; i++) {
                childArray[i] = arguments[i + 2];
            }
            props.children = childArray;
        }      
     // 三、處理type的 defaultProps
        if(type && type.defaultProps) {
            const defaultProps = type.defaultProps;
            for (propName in defaultProps) {
                if(props[propName] === undefined) {
                    props[propName] = defaultProps[propName]
                }
            }
        }
        
   // 四、返回一個(gè)ReactElement()函數(shù)
         return ReactElement(
            type,
            key,
            ref,
            self,
            source,
            ReactCurrentOwner.current,
            props,
          );
} 

注:type.defaultProps的由來:

  1. 定義一個(gè)ClassComponent: class Comp extends React.Component
  2. 可以為 Comp設(shè)置一個(gè)defaultProps: Comp.defaultProps = { value: 1 }
//源碼位置: packages/react/src/ReactElement.js 
// ReactElement 函數(shù)
const ReactElement = function(type, key, ref, self, source, owner, props){
    const element = {
         // $$typeof用于標(biāo)識 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,
    }
    
    // _DEV_ 代碼...
    
    return element;
}

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多