同作为MVVM框架,React相比于Vue来讲,上手更需要JavaScript
功底深厚一些,本系列将阅读React
相关源码,从jsx -> VDom -> RDOM
等一些列的过程,将会在本系列中一一讲解
工欲善其事,必先利其器
经过多年的发展,React已经更新了大版本16、17、18
,本系列主要讲的是 version:17.0.2
,在讲这个版本之前,我们先看一看在babel
的编译下,每个大版本之下会有什么样的变化。
jsx
<div className='box'>
<h1 className='title' style={
{
'color':'red'}}>React源码解析</h1>
<ul>
<li>第一章</li>
<li>第二章</li>
<li>第三章</li>
<li>第四章</li>
</ul>
</div>
v16.x及以前版本
v17及之后版本
所以各位看到了,在v16
及以前我们babel
进行jsx
解析编译的是根据@babel/babel-preset-react-app解析成React.createElement
进行包裹的,而v17
以及之后的版本,官网早就说明,对jsx
的转换用react/jsx-runtime
,而不再依赖React.createElement
了,看到这里我想各位对不同版本的babel解析jsx已经有了眉目了,早已经迫不及待想去看看jsx-runtime和createElement到底是如何玩的,那么进入源码
在babel解析后的v17产物中我们可以看得到 var _jsxRuntime = require("react/jsx-runtime");
那么我们追本溯源可以找到在packages/react/src/jsx/ReactJSX.js
里面的jsxs
是怎么来的
// packages/react/src/jsx/ReactJSX.js
import {
REACT_FRAGMENT_TYPE} from 'shared/ReactSymbols';
import {
jsxWithValidationStatic,
jsxWithValidationDynamic,
jsxWithValidation,
} from './ReactJSXElementValidator';
import {
jsx as jsxProd} from './ReactJSXElement';
const jsx = __DEV__ ? jsxWithValidationDynamic : jsxProd;
const jsxs = __DEV__ ? jsxWithValidationStatic : jsxProd;
const jsxDEV = __DEV__ ? jsxWithValidation : undefined;
export {
REACT_FRAGMENT_TYPE as Fragment, jsx, jsxs, jsxDEV};
在非dev
环境下我们继续去找jsProd
export function jsx(type, config, maybeKey) {
let propName;
//标签上的属性集合
const props = {
};
//单独处理key ref
let key = null;
let ref = null;
if (maybeKey !== undefined) {
key = '' + maybeKey;
}
if (hasValidKey(config)) {
// 处理合法的key
key = '' + config.key;
}
if (hasValidRef(config)) {
// 处理合法的ref
ref = config.ref;
}
// 把属性加到props中
for (propName in config) {
if (
hasOwnProperty.call(config, propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)
) {
props[propName] = config[propName];
}
}
// 处理默认props
if (type && type.defaultProps) {
const defaultProps = type.defaultProps;
for (propName in defaultProps) {
if (props[propName] === undefined) {
props[propName] = defaultProps[propName];
}
}
}
return ReactElement(
type,
key,
ref,
undefined,
undefined,
ReactCurrentOwner.current,
props
)
}
ReactElement
const ReactElement = function(type, key, ref, self, source, owner, props) {
const element = {
// 表示是否为ReactElement
$$typeof: REACT_ELEMENT_TYPE,
// 元素自身属性
type: type,
key: key,
ref: ref,
props: props,
// Record the component responsible for creating this element.
_owner: owner,
};
if (__DEV__) {
element._store = {
};
// 开发环境下将_store、_self、_source属性变为不可枚举
Object.defineProperty(element._store, 'validated', {
configurable: false,
enumerable: false,
writable: true,
value: false,
});
Object.defineProperty(element, '_self', {
configurable: false,
enumerable: false,
writable: false,
value: self,
});
Object.defineProperty(element, '_source', {
configurable: false,
enumerable: false,
writable: false,
value: source,
});
// 冻结props、element防止被手动修改
if (Object.freeze) {
Object.freeze(element.props);
Object.freeze(element);
}
}
return element;
};
这上面便是v17
及之后版本的jsx-runtime
所做的事情。那么这里再去看一下v16
中的createElement
所做的事情吧。相关参考视频讲解:进入学习
React.createElement
// packages/react/src/ReactElement.js
export function createElement(type, config, children) {
let propName;
// 记录标签上的属性集合
const props = {
};
//单独处理key ref
let key = null;
let ref = null;
let self = null;
let source = null;
// 当config部位null的时候,表示标签上有属性,加到props里面去
if (config != null) {
// 合法的ref才做处理
if (hasValidRef(config)) {
ref = config.ref;
if (__DEV__) {
warnIfStringRefCannotBeAutoConverted(config);
}
}
if (hasValidKey(config)) {
// 有合法的key才做处理
key = '' + config.key;
}
// 记录信息用于debug
self = config.__self === undefined ? null : config.__self;
source = config.__source === undefined ? null : config.__source;
// 处理self,source,key,ref以外的属性,加入props中
for (propName in config) {
if (
hasOwnProperty.call(config, propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)
) {
props[propName] = config[propName];
}
}
}
// 处理子节点
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];
}
//开发环境冻结,childArray防止被修改
if (__DEV__) {
if (Object.freeze) {
Object.freeze(childArray);
}
}
props.children = childArray;
}
// 处理默认props
if (type && type.defaultProps) {
const defaultProps = type.defaultProps;
for (propName in defaultProps) {
if (props[propName] === undefined) {
props[propName] = defaultProps