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

你可能感兴趣的文章
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>