博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实战json、html、jsx的互转
阅读量:2355 次
发布时间:2019-05-10

本文共 3102 字,大约阅读时间需要 10 分钟。

jsx2html

场景

将下面的jsx转换为html

const items = ['one', 'two', 'three'];const SearchData = ({ data = [] }) => {  let list = null;  if (data.length) {    list = data.map((l, k) => (

{l}

)); } else { list = (

暂无数据

); } return (

匹配的员工:

{list}
);};const jsx = (

列表展示 {items.length} 项:

SEARCH DATA:

);

方案

方案一:自己遍历

所有的jsx你拿到的时候,都已经是 Babel 帮你转义过的了。所以,你其实拿到的是转义后的对象。所以你只需要将这对象转成你想要的结果。我们知道 Props 除了 key、ref、children 这几个特殊的,剩下的都对应到 dom 的 。

做法

1、获取 displayName 作为 Tag
2、处理 props 作为 attribute
3、如果有children,则重复1、2、3、4,遍历 children,作为子元素
4、拼装 html 字符串

代码
function getDisplayName(ele) {  if (typeof ele.type === 'string') {    return ele.type;  }  return ele.type.name || ele.type.displayName || 'No Name';}function getAttrs(attrs) {  return Object.keys(attrs).map(attr => (attr === 'children' ? '' : `${attr}="${attrs[attr]}"`)).join('');}function transfer(ele) {  if (typeof ele === 'string' || typeof ele === 'number') {    return ele;  }  const props = ele.props || {};  const children = React.Children.toArray(props.children || []);  const html = children.map(transfer);  const tag = getDisplayName(ele);  return `<${tag} ${getAttrs(props)}>${html.join('')}
`;}console.log(transfer(jsx));// 如果函数式的组件你也需要解析的话,则需要执行这个函数// congsole.log(transfer(SearchData({items: data})))
类库

方案二:通过 Babel 直接转

我们可以指定 Babel 在编译的时候调用某个函数。我们可以通过这个函数来生成我们需要的操作。

做法
  • 方法一 配置 json { "plugins": [ ["transform-react-jsx", { "pragma": "dom" // default pragma is React.createElement }] ] }

但是这种方法修改全部的 babel 转换行为。非常不推荐

  • 方法二

在代码中加一个注释/** @jsx h */,告诉 Babel ,用 h 函数处理 Babel 编译后的行为。参考

代码
/** @jsx h */function getDisplayName(ele) {  if (typeof ele === 'string') {    return ele;  }  return ele.name || ele.displayName || 'No Name';}function h(name, attrs, ...children) {  const html = Array.isArray(children) ? children.join('') : children;  console.log('###################################');  console.log('name:', name);  console.log('attrs:', attrs);  console.log('children:', children);  const attr = Object.keys(attrs || {}).map(a => `${a}='${attrs[a]}'`).join(' ');  return `<${name} ${attr}>${html}
`;}console.log(jsx);
类库

jsx2json

与上面的情况了类似,如果我们要将那部分 jsx 转换为 json 格式的怎么办呢?答案很简单,不用刻意去转(?!)。因为 Babel 已经帮你转过了。你需要做的是把 Babel 编译后的 json 转成你想要的格式。此外,刚才的两种方案也是生效的。不同的是,之前的返回值是一段 html 文本,现在需要返回 json 格式的。我们以上面的方案二举例:

/** @jsx h */function h(name, attrs, ...children) {  /*  // 函数式的组件(functional component)请根据需要转换  if (typeof name === 'function') {    return name(attrs);  }  */  return {    tag: name,    attrs,    children,  };}console.log(jsx);

html2jsx

场景

将下面的 html 转换为 jsx:

const html = `  

Hi!

Here is a list of 3 items:

  • one
  • two
  • three
`;

方案

将 html 转为 jsx,实际上就是用 React.createElement 将 html 的结构重新生成一下,做到

做法

1、将 html 片段转成 dom
2、读取 dom 的 attributes, 处理特殊的 attribute,作为 ReactElement 的 props
3、读取 dom 的 tagName, 作为 ReactElement 的 type
4、如果 dom 有 children,则重复 2、3、5步,将返回值作为 ReactElement 的 children
5、返回 React.createElement(type, props, children)

转载地址:http://vdktb.baihongyu.com/

你可能感兴趣的文章
python的Pattern模块
查看>>
关于hive同一个脚本运行多次而每次结果都不相同
查看>>
多种get_post提交数据到网络
查看>>
推送通知的两种编写方式
查看>>
短信发送器(界面传递数据)
查看>>
加载图片到内存中
查看>>
图片的镜子效果
查看>>
图片的拷贝
查看>>
图片的旋转
查看>>
图片的颜色变化
查看>>
撕衣服案例
查看>>
触摸事件
查看>>
学生管理系统(LinearLayout)
查看>>
回调方法callback
查看>>
绑定(远程)服务
查看>>
带进度条的多线程断点下载
查看>>
使用开源组件进行断点下载
查看>>
自定义控件和试图(原生api)优酷菜单
查看>>
自定义控件和试图(原生api)viewPager图片轮播广告
查看>>
使用v4包或者其他第三方包的注意事项
查看>>