createElement执行过程

createElement

  • createElement(type, config, children)
    
    • config

      • 是否存在key 并给其放入提前定义好的key中

        if (hasValidKey(config)) {  key = '' + config.key;}
        
      • 遍历config 将不是RESERVED_PROPS里面包含的属性遍历存入props

      for (propName in config) { 
          if (    hasOwnProperty.call(config, propName) &&    !RESERVED_PROPS.hasOwnProperty(propName)  ) {  
              props[propName] = config[propName];  }}
      
    • children

      • 找出children的长度

        const childrenLength = arguments.length - 2;
        
      • 并将children内容放入props.children里面

        if (childrenLength === 1) { //长度为1直接放入
            props.children = children;
        } else if (childrenLength > 1) {//长度大于1 遍历成数组,将数组放入
            const childArray = Array(childrenLength);//定义数组长度
            for (let i = 0; i < childrenLength; i++) {
                childArray[i] = arguments[i + 2];
            }  if (__DEV__) { 
                if (Object.freeze) {      Object.freeze(childArray);    }  } 
            props.children = childArray;}
        
    • type

      • 对默认值的特殊处理
      if (type && type.defaultProps) {//存在默认值
          const defaultProps = type.defaultProps; 
          for (propName in defaultProps) { 
              if (props[propName] === undefined) {  //默认值属性在props没有定义
                  props[propName] = defaultProps[propName];//给其定义    } 
       }}
      
  • 处理完数据后

return ReactElement(  type,  key,  ref,  undefined,  undefined,  ReactCurrentOwner.current,  props,);//将处理后的数据返回给ReactElement 方法
  • ReactElement 方法

    • const element = {  // This tag allows 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;
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值