React源码阅读--React.createElement和ReactElement

18 篇文章 1 订阅

React.createElement

在React中,JSX语法糖都会被转换为React.createElement的形式。

例如:

<div id='div'>hello</div>

会被转换为:

React.createElement('div', {id: 'div'}, 'hello');

这个方法是React对象的一个方法,在源码目录下的React.js中,可以看见React对象内包含了这个方法。

在这里插入图片描述

源码

而这个方法真正的定义则是在ReactElement.js文件内。

export function createElement(type, config, children) {
  let propName;

  // Reserved names are extracted
  const props = {};

  let key = null;
  let ref = null;
  let self = null;
  let source = null;

  if (config != null) {
    if (hasValidRef(config)) {
      ref = config.ref;
    }
    if (hasValidKey(config)) {
      key = '' + config.key;
    }

    self = config.__self === undefined ? null : config.__self;
    source = config.__source === undefined ? null : config.__source;
    // Remaining properties are added to a new props object
    for (propName in config) {
      if (
        hasOwnProperty.call(config, propName) &&
        !RESERVED_PROPS.hasOwnProperty(propName)
      ) {
        props[propName] = config[propName];
      }
    }
  }

  // Children can be more than one argument, and those are transferred onto
  // the newly allocated props object.
  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];
    }
    if (__DEV__) {
      if (Object.freeze) {
        Object.freeze(childArray);
      }
    }
    props.children = childArray;
  }

  // Resolve default props
  if (type && type.defaultProps) {
    const defaultProps = type.defaultProps;
    for (propName in defaultProps) {
      if (props[propName] === undefined) {
        props[propName] = defaultProps[propName];
      }
    }
  }
  if (__DEV__) {
    if (key || ref) {
      const displayName =
        typeof type === 'function'
          ? type.displayName || type.name || 'Unknown'
          : type;
      if (key) {
        defineKeyPropWarningGetter(props, displayName);
      }
      if (ref) {
        defineRefPropWarningGetter(props, displayName);
      }
    }
  }
  return ReactElement(
    type,
    key,
    ref,
    self,
    source,
    ReactCurrentOwner.current,
    props,
  );
}

首先方法内定义了一个props对象,用以存放我们为组件传入的props。需要注意的是,config参数并不一定就直接是最终的props,因为keyref都不是传给组件的。后面定义的keyref变量就是用来存放这两个值的。

接下来

if (hasValidRef(config)) {
    ref = config.ref;
}
if (hasValidKey(config)) {
    key = '' + config.key;
}

这两个if分别判断传入的config参数内包不包含合法的ref和合法的key,若包含则分别用ref变量、key变量保存下来。

然后

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

这段代码是负责将config参数中除去keyref的值放入之前定义的props对象中。hasOwnProperty用以确认当前的键是定义在config上而不是其原型链上。

!RESERVED_PROPS.hasOwnProperty(propName)则是排除掉不属于props对象的属性。RESERVED_PROPS的定义如下:

const RESERVED_PROPS = {
  key: true,
  ref: true,
  __self: true,
  __source: true,
};

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];
    }
    if (__DEV__) {
      if (Object.freeze) {
        Object.freeze(childArray);
      }
    }
    props.children = childArray;
  }

从JSX转换成JavaScript之后的代码可以看出,React.createElement方法从第三个参数开始,之后所有的参数都是该组件的子组件。

所以arguments.length-2就是在计算子组件的个数。若只有一个子组件,那么props.children的值就是这个子组件。若大于一个,则将所有子组件都放进一个数组内。


if (type && type.defaultProps) {
    const defaultProps = type.defaultProps;
    for (propName in defaultProps) {
      if (props[propName] === undefined) {
        props[propName] = defaultProps[propName];
      }
    }
  }

我们在编写组件的时候可以通过编写defaultProps来为组件设置缺省的props。而这个缺省的props就是在这里被设置的。

可以看出,先判断有没有defaultProps,若有接着判断defaultProps中的每一个键,若该键在之前定义的props对象中找不到对应的值,则将defaultProps中的对应值赋给props对象。这样就实现了“若传入的props有对应值就是用传入的,若没有则使用默认的”的效果了。


最后就是返回ReactElement方法返回的结果。

ReactElement

const ReactElement = function(type, key, ref, self, source, owner, props) {
  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,
  };

  if (__DEV__) {
    // The validation flag is currently mutative. We put it on
    // an external backing store so that we can freeze the whole object.
    // This can be replaced with a WeakMap once they are implemented in
    // commonly used development environments.
    element._store = {};

    // To make comparing ReactElements easier for testing purposes, we make
    // the validation flag non-enumerable (where possible, which should
    // include every environment we run tests in), so the test framework
    // ignores it.
    Object.defineProperty(element._store, 'validated', {
      configurable: false,
      enumerable: false,
      writable: true,
      value: false,
    });
    // self and source are DEV only properties.
    Object.defineProperty(element, '_self', {
      configurable: false,
      enumerable: false,
      writable: false,
      value: self,
    });
    // Two elements created in two different places should be considered
    // equal for testing purposes and therefore we hide it from enumeration.
    Object.defineProperty(element, '_source', {
      configurable: false,
      enumerable: false,
      writable: false,
      value: source,
    });
    if (Object.freeze) {
      Object.freeze(element.props);
      Object.freeze(element);
    }
  }

  return element;
};

一眼就可以看出,ReactElement方法返回的就是一个普通的JS对象。这个对象内的值基本就是刚刚React.createElement中传入的那些。

这里面要注意的是$$typeof这个属性,它用来标记该对象是一个React元素。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值