全文连接html
将下面的jsx转换为htmlnode
const items = ['one', 'two', 'three'];const SearchData = ({ data = [] }) => { let list = null; if (data.length) { list = data.map((l, k) => (<p key={k}>{l}</p>)); } else { list = (<p>暂无数据</p>); } return ( <div className="mod-search-data"> <div> <h3>匹配的员工:</h3> <div>{list}</div> </div> </div> ); };const jsx = ( <div className="foo"> <p>列表展现 {items.length} 项:</p> <div> <p>SEARCH DATA:</p> <SearchData data={items} /> </div> </div>);
全部的jsx你拿到的时候,都已是 Babel 帮你转义过的了。因此,你其实拿到的是转义后的对象。因此你只须要将这对象转成你想要的结果。咱们知道 Props 除了 key、ref、children 这几个特殊的,剩下的都对应到 dom 的 attribute。react
一、获取 displayName 做为 Tag
二、处理 props 做为 attribute
三、若是有children,则重复一、二、三、4,遍历 children,做为子元素
四、拼装 html 字符串git
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('')}</${tag}>`; }console.log(transfer(jsx));// 若是函数式的组件你也须要解析的话,则须要执行这个函数// congsole.log(transfer(SearchData({items: data})))
react-element-to-jsx-stringgithub
咱们能够指定 Babel 在编译的时候调用某个函数。咱们能够经过这个函数来生成咱们须要的操做。json
方法一 配置 .babelrcjson { "plugins": [ ["transform-react-jsx", { "pragma": "dom" // default pragma is React.createElement }] ] }
babel
可是这种方法修改所有的 babel 转换行为。很是不推荐dom
方法二ide
在代码中加一个注释/** @jsx h */,告诉 Babel ,用 h 函数处理 Babel 编译后的行为。参考WTF is JSX函数
/** @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}</${name}>`; }console.log(jsx);
与上面的状况了相似,若是咱们要将那部分 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);
将下面的 html 转换为 jsx:
const html = ` <div className='foo' id="18" data-node="12"> <h1>Hi!</h1> <p>Here is a list of 3 items:</p> <ul> <li>one</li> <li>two</li> <li>three</li> </ul> </div>`;
将 html 转为 jsx,实际上就是用 React.createElement 将 html 的结构从新生成一下,作到without jsx
一、将 html 片断转成 dom二、读取 dom 的 attributes, 处理特殊的 attribute,做为 ReactElement 的 props三、读取 dom 的 tagName, 做为 ReactElement 的 type四、若是 dom 有 children,则重复 二、三、5步,将返回值做为 ReactElement 的 children五、返回 React.createElement(type, props, children)