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,因为key
和ref
都不是传给组件的。后面定义的key
和ref
变量就是用来存放这两个值的。
接下来
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
参数中除去key
、ref
的值放入之前定义的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
元素。