/**
* @method createElement
* @param type {string}
* @param props {Object}
* @param children {string}
*/
let createElement = (type, props, ...children) => { props = props || {};
let obj = {
type: null,
props: {
children: children.length <= 1 ? (children[0] || '') : children
},
key: null,
ref: null,
};
obj = {...obj, type, props: {...props, children}};
// eslint-disable-next-line no-unused-expressions
'key' in obj.props ? (obj.key = obj.props.key, obj.props.key = undefined) : null;
// eslint-disable-next-line no-unused-expressions
'ref' in obj.props ? (obj.ref = obj.props.ref, obj.props.ref = undefined) : null;
return obj;
}; /**
* @method render
* @param obj {Object}
* @param container {elementNode}
* @param callback {function}
*/
let render = (obj, container, callback) => { let {type, props} = obj || {},
newElement = document.createElement(type); for (const attr in props) { if (!props.hasOwnProperty(attr)) break; // 不是私有的直接结束遍历
if (!props[attr]) continue; // 如果当前属性没有值,直接不处理即可
let value = props[attr]; // className
if (attr === 'className') {
newElement.setAttribute('class', value);
continue
} // style
if (attr === 'style') {
// 如果style为空支付串,不处理
if (value === '') continue;
for (const styleKey in value) {
if (value.hasOwnProperty(styleKey)) {
newElement['style'][styleKey] = value[styleKey];
}
}
continue
} // children
if (attr === 'children') { // 先将其转换为数组,后期直接操作数组即可
// eslint-disable-next-line no-unused-expressions
!(value instanceof Array) ? value = [value] : null;
value.forEach((item, index) => {
if (typeof item === "string") {
let text = document.createTextNode(item);
newElement.appendChild(text) } else {
render(item, newElement);
}
});
continue
}
newElement.setAttribute(attr, value); // 基于setAttribute设置的属性,可以变现在HTML的结构上
} container.appendChild(newElement);
callback && callback();
}; let elementNode = createElement(
'div',
{id: 'container', ref: 'main', key: new Date()},
createElement(
'hr'
),
createElement(
'div',
{id: 'header'},
'头部'
),
createElement(
'hr'
),
createElement(
'div',
{id: 'main'},
'主体内容'
),
createElement(
'hr'
),
createElement(
'div',
{id: 'footer'},
'脚注'
),
); // eslint-disable-next-line no-undef
render(elementNode, root, _ => console.log('ok'))

简单实现react中虚拟DOM渲染的更多相关文章

  1. react中虚拟dom的diff算法

    .state 数据 .jsx模板 .生成虚拟dom(虚拟DOM就是一个js对象,用它来描述真实DOM) ['div', {id:'abc'}, ['span', {}, 'hello world']] ...

  2. 聊一聊React中虚拟DOM

    1. 什么是虚拟 DOM 在 React 中实际上是 render 函数中return 的内容会生成 DOM,return 中的内容由两部分组成,一部分是 JSX ,另一部分就是 state 中的数据 ...

  3. react中虚拟DOM的基本概念

    react中的核心概念 1.DOM的本质是什么: 浏览器中的概念,用js对象来表示页面上的元素,并提供操作DOM对象的API 2.什么事react中的虚拟DOM:是框架中的概念,是程序员用js对象来模 ...

  4. react中虚拟DOM

    简单来说虚拟DOM就是一个js对象,相对于真实dom来做比较更节约性能,虚拟DOM执行过程如下

  5. 【React自制全家桶】二、分析React的虚拟DOM和Diff算法

    一.React如何更新DOM内容: 1.  获取state 数据 2.  获取JSX模版 3.  通过数据 +模版结合,生成真实的DOM, 来显示,以下行代码为例(简称代码1) <div id= ...

  6. React的虚拟DOM

    ReactJs的一大特点就是引进了虚拟dom(Virtual DOM)的概念.为什么我们需要Virtual DOM,Virtual DOM给我们带来了什么优势. 首先我们要了解一下浏览器的工作流. 当 ...

  7. vue2.0的虚拟DOM渲染

    1.为什么需要虚拟DOM 前面我们从零开始写了一个简单的类Vue框架(文章链接),其中的模板解析和渲染是通过Compile函数来完成的,采用了文档碎片代替了直接对页面中DOM元素的操作,在完成数据的更 ...

  8. 【译】在React中实现条件渲染的7种方法

    原文地址:https://scotch.io/tutorials/7-ways-to-implement-conditional-rendering-in-react-applications 借助R ...

  9. React virtual DOM explained in simple English/简单语言解释React的虚拟DOM

    初学React,其中一个很重要的概念是虚拟DOM,看了一篇文章,顺带翻译一下. If you are using React or learning React, you must have hear ...

随机推荐

  1. 注册emf package并读取EMF文件

    /** * 读EMF文件 * * @param uri * @return */ public static Resource readEMFFile(URI uri) { ResourceSet r ...

  2. 2019ICPC区域赛(银川)总结

    2019ICPC银川 作为第一次打区域赛的我,心情异常激动,加上学校给坐飞机(事实上赶飞机很痛苦). 热身赛很难受,oj上不去,写AC自动机输入没写好.. 现场赛,开场直觉倒着看,发现签到.然后看B, ...

  3. stl常数测试

    如图: 数组的常数约为9.

  4. this绑定问题

    this是属性和方法“当前”(运行时)所在的对象.this是函数调用时发生的绑定,它的值只取决于调用位置(箭头函数除外). 函数调用的时候会产生一个执行上下文,this是对这个执行上下文的记录. ❌误 ...

  5. SharePoint學習

    1.SharePoint 2010 Products -> SharePoint 2010 Products Configuration Wizard    配置好后,系統會自動在localho ...

  6. Linq to JSON/Json.NET

    Can I LINQ a JSON? http://stackoverflow.com/questions/18758361/can-i-linq-a-json Json.NET https://js ...

  7. STS工具各版本下载网址

    官网网址:https://spring.io/tools3/sts/legacy

  8. leetcode解题报告(6):Remove Duplicates from Sorted List

    描述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...

  9. ListCtrl 技巧集

    1. ListCtrl 风格       LVS_ICON: 为每个item显示大图标       LVS_SMALLICON: 为每个item显示小图标       LVS_LIST: 显示一列带有 ...

  10. laotech老师唠科mac 深入浅出MAC OS X ceshi ruguokeyi

    laotech老师唠科mac 深入浅出MAC OS X http://study.163.com/plan/planLearn.htm?id=1637004#/learn/resVideo?lesso ...