博客
关于我
ReactElement源码理解
阅读量:734 次
发布时间:2019-03-22

本文共 2496 字,大约阅读时间需要 8 分钟。

ReactElement 源码笔记

ReactElement通过 createElement创建,调用该方法需要 传入三个参数:

  • type
  • config
  • children

type指代这个ReactElement 的类型

config指代这个ReactElement 的属性对象

children指代这个ReactElement 的子元素节点

  • 字符串比如 'div''p'代表原生DOM,称为HostComponent
  • Class 类型是我们继承自 Component 或者 PureComponent的组件,称为ClassComponent
  • 方法就是 function Component
  • 原生提供的 FragmentAsyncMode 等是 Symbol,会被特殊处理
  • TODO: 是否有其他的
//源码位置: packages/react/src/ReactElement.js   // createElement函数export function createElement(type, config, children){    // 一、处理config        // 1. 判断config是否为空        // 2. 提取保留属性(key, ref, self, soure)        // 3. 其余属性添加到新的props对象中        for(propName in config){            if(                hasOwnProperty.call(config, propName)&&                !RESERVED_PROPS.hasOwnProperty(propName)            ){                props[propName] = config[propName]            }        }        // 二、处理children => props.children    // (children可以超过一个),处理过后的children 被放入 props.children        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];            }            props.children = childArray;        }	           // 三、处理type的 defaultProps        if(type && type.defaultProps) {            const defaultProps = type.defaultProps;            for (propName in defaultProps) {                if(props[propName] === undefined) {                    props[propName] = defaultProps[propName]                }            }        }           	// 四、返回一个ReactElement()函数         return ReactElement(            type,            key,            ref,            self,            source,            ReactCurrentOwner.current,            props,          );}

注:type.defaultProps的由来:

  1. 定义一个ClassComponent: class Comp extends React.Component
  2. 可以为 Comp设置一个defaultProps: Comp.defaultProps = { value: 1 }
//源码位置: packages/react/src/ReactElement.js // ReactElement 函数const ReactElement = function(type, key, ref, self, source, owner, props){    const element = {         // $$typeof用于标识 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,    }        // _DEV_ 代码...        return element;}

转载地址:http://vgkwk.baihongyu.com/

你可能感兴趣的文章
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>