JSON2DOM = react的render函数

文章目录

  • JSON2DOM = react的render函数
    • 功能介绍
    • 实现 _render 函数
    • 测试结果

功能介绍

const vnode = {tag: 'DIV',attrs: {id: 'app'},children: [{tag: 'SPAN',children: [{tag: 'A',children: ["text1"]}]},{tag: 'DIV',children: [{tag: 'SECTION',attrs: {class: 'box','data-birthday': '02-17',},children: [666,{tag: 'h3',children: ["text2"]},{tag: 'h4',children: ["text3"]},{tag: 'h5',children: ["text4"]}]},{tag: 'A',attrs: {class: 'link',style: {color: 'red','text-decoration': 'underline'}},children: [[1, 2, 3]]}]}]
}
// 转化为下述 DOM 结构
<div id="app"><span><a>text1</a></span><div><section class="box" data-birthday="02-17">666<h3>text2</h3><h4>text3</h4><h5>text4</h5></section><a class="link" style="color: red; text-decoration: underline;">1,2,3</a></div>
</div>

实现 _render 函数


function _render(vnode) {if (vnode === null || typeof vnode !== 'object') {return document.createTextNode(vnode);}if (Array.isArray(vnode)) {return document.createTextNode(vnode.toString());}let root = null;if (vnode.tag) {root = document.createElement(vnode.tag);}if (vnode.attrs) {Object.entries(vnode.attrs).forEach(item => {if (item[0] !== 'style') {root.setAttribute(item[0], item[1]);}else {root.style.cssText = Object.entries(item[1]).reduce((prev, curr) => (prev += `${curr[0]}:${curr[1]};`, prev), '');}})}if (vnode.children) {vnode.children.forEach(item => root.appendChild(_render(item)));}return root;
}

测试结果

react 的 render 函数相关推荐

  1. [react] render函数中return如果没有使用()会有什么问题?

    [react] render函数中return如果没有使用()会有什么问题? 我们在使用JSX语法书写react代码时,babel会将JSX语法编译成js,同时会在每行自动添加分号(:),如果retu ...

  2. 在React的render方法中使用箭头函数

    在 React 组件中绑定点击事件通常的做法使用 class 属性. class Foo extends Component {handleClick = () => {console.log( ...

  3. React的render不是纯函数? triggering nested component updates from render is not allowed

    "Warning: Render methods should be a pure function of props and state; triggering nested compon ...

  4. react中纯函数_如何在纯React中创建电子邮件芯片

    react中纯函数 by Andreas Remdt 由Andreas Remdt 如何在纯React中创建电子邮件芯片 (How to create email chips in pure Reac ...

  5. React 万能的函数表达式

    一.语法简介 表达式可以以下两种方法, (1)(function A(){})(this),(this)参数在函数外面 (2)(function B(){}(this)),(this)参数在函数里面 ...

  6. 你需要知道的vue2 jsx render函数

    通常开发vue我们使用的是模板语法,其实还有和react相同的语法,那就是render函数,同样支持jsx语法. Vue 的模板实际是编译成了 render 函数. 0 传统的createElemen ...

  7. [react] 请说说你对react的render方法的理解

    [react] 请说说你对react的render方法的理解 render是class组件中必须被重载的方法,组件执行render方法的条件如下: 初始渲染 this.setState方法,参数不能为 ...

  8. TypeScript + React 学习render props

    ###前言 一直想学学TypeScript,尝试尝试带类型的js怎么写,有啥优点.正好这两天有时间结合着react写写小例子. 搭建环境 不想折腾webpack来自己配ts+react的环境就用typ ...

  9. react 绑定 箭头函数_为什么箭头函数和React渲染中的绑定有问题

    react 绑定 箭头函数 (提示:这会使shouldComponentUpdate和PureComponent变得胡思乱想) ((Hint: It makes shouldComponentUpda ...

最新文章

  1. power 芯片 运行linux,IBM Power系列的是不是只能用linux?
  2. 使用bash工具创建ssh key
  3. java练习:打印 数字1、2、3、4的组合,不能以4开头,1和3不能相邻,且数字不能重复
  4. linux 安全基线加固
  5. Halcon知识:如何求一个工件的粗细
  6. 使用TCP可靠吗?为什么可靠?
  7. python3软件怎么用_Python3学习之路~4.4 软件目录结构规范
  8. XSS与CSRF两种跨站攻击比较
  9. plt.savefig 不保存边框_Matlab科研绘图 amp; 保存
  10. Luogu P2733 家的范围 Home on the Range
  11. 数据库工作笔记018---Windows下mysql安装_服务无法启动没有报告解决
  12. FOSRestBundle功能包:使用指南
  13. Spring源码解析(一)下载及编译(版本5.2.x)
  14. Nike Air Shake ndestrukt white red Performance Reviews
  15. 简单A/BTest验证图片懒加载效果
  16. java源码转换e4a_E4A核心代码生成类库工具
  17. 微电子电路——期末总结
  18. oa html5管理系统模板,OA系统html5整站模板 后台模板
  19. 并口st7789v2_3.2寸240*320,ST7789V,8080 8//16位MCU并口3/4SPI+16/18 bit RGB ,全彩液晶显示模块...
  20. 浪潮ClusterEngineV4.0 任意用户登录漏洞

热门文章

  1. 数据分析的年度工作计划,这样制定才合理!
  2. android WIFI学习总结
  3. 异构数据源之间交换数据的工具
  4. 微信传文件又慢又限制大小?试试这3个免费在线传文件工具!
  5. leetcode 5473. 灯泡开关 IV medium (智力题,O(n)思路详解)
  6. 抖音做我女朋友的 vbs 脚本
  7. 【VRP问题】基于遗传算法求解带容量的VRP问题matlab源码
  8. 个人认为安卓开发前景
  9. BATCH/批处理命令
  10. MySQL安装版本Navicat连接报错2509解决方案