一、React.createElement(type,config[…children])源码分析
在react中,通过createElement进行虚拟dom的创建,createElement接收三个参数,
(类型,属性,子类)= (type, config, children)
export function createElement(type, config, children) {
// type的值可能为 function, text,div等
let propName;
// 提取保留名称
const props = {};
let key = null;
let ref = null;
let self = null;
let source = null;
// 标签的属性不为空时 说明标签有属性值 特殊处理:把key和ref赋值给单独的变量
if (config != null) {
if (hasValidRef(config)) {
// 如果有ref将他取出来
ref = config.ref;
}
if (hasValidKey(config)) {
// 如果有key将他取出来
key = '' + config.key;
}
self = config.__self === undefined ? null : config.__self;
source = config.__source === undefined ? null : config.__source;
//config中剩余属性,且不是原生属性(RESERVED_PROPS对象的属性),则添加到新props对象中
for (propName in config) {
if (
hasOwnProperty.call(config, propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)
) {
// 将除ref,key这类特殊属性的其他属性放到新的props对象里
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); //声明一个数组
//依次将children push到数组中
for (let i = 0; i < childrenLength; i++) {
childArray[i] = arguments[i + 2];
}
if (__DEV__) {
// 冻结array 返回原来的childArray且不能被修改
if (Object.freeze) {
Object.freeze(childArray);
}
}
// 父组件内部通过this.props.children获取子组件的值
props.children = childArray;
}
// Resolve default props
// 添加默认props 一般针对的是组件
if (type && type.defaultProps) { //如果当前组件中有默认的defaultProps则把当前组件的默认内容 定义到defaultProps中
const defaultProps = type.defaultProps;
for (propName in defaultProps) {
if (props[propName] === undefined) { //如果父组件中对应的值为undefinde 则把默认值赋值赋值给props当作props的属性
props[propName] = defaultProps[propName];
}
}
}
if (__DEV__) {
// 存在ref或者key时
if (key || ref) {
// 如果type是组件的话
const displayName =
typeof type === 'function'
? type.displayName || type.name || 'Unknown'
: type;
if (key) {
defineKeyPropWarningGetter(props, displayName);
}
if (ref) {
defineRefPropWarningGetter(props, displayName);
}
}
}
// 执行ReactElement
return ReactElement(
type,
key,
ref,
self,
source,
ReactCurrentOwner.current,
props,
);
}
二、ReactElement
作用:返回一个虚拟dom对象
const ReactElement = function(type, key, ref, self, source, owner, props) {
// 最终得到的React元素
const element = {
// 因为react最终渲染dom时候,确保是react.createElement类型 需要判断$$typeof===REACT_ELEMENT_TYPE
$$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) {
// 代码性能优化:将element的一些属性配置为不可配置 提高性能
Object.freeze(element.props);
Object.freeze(element);
}
}
return element; // 返回虚拟dom对象
};