react源码分析:babel如何解析jsx

本文深入探讨了React源码,重点关注jsx的编译过程,React.createElement的实现,以及React组件的生命周期。文章揭示了不同React版本中jsx的处理方式,详细解析了ReactElement和React.Component的内部工作原理,并概述了事件系统的处理流程,包括事件注册、优先级管理和调度。
摘要由CSDN通过智能技术生成

同作为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] = defaultPro
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值