文章目录

因为在项目中用React用的比较多,准备开个专栏来研究react的设计结构及源码。

我们从下面这个简单的例子入手来看虚拟DOM和react的构造机制。

React.render(React.createElement('div', {name: "John"}), document.getElementById("container"));
由此我们可以开始定义及猜测React的基本结构
React{
   render:function(todoElement,targetNode){},
   createElement:function(type,config,children){}
};
  • render方法是将第一个参数todoElement改造成html,放入第二个对应的容器中。
  • createElement将参数改造成虚拟dom,供render函数调用。为保证信息的完整性,可以推知虚拟Dom的创建必须满足如下要求:

    虚拟dom包含信息>=真实DOM包含信息

    初步定义虚拟dom结构:
    {
    type:type,//可以是html标签如’div’或者是’function’构造函数形式
    props:props,//包含html的所有attribute和子元素children
    key:key//html的映射键值
    };

    这时候,在render函数体内,接收到的参数就是虚拟dom和目标容器。

文章目录