博客
关于我
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/

你可能感兴趣的文章
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
查看>>
mysql中实现rownum,对结果进行排序
查看>>