将函数转换为jsx

时间:2018-05-22 13:56:51

标签: javascript reactjs

我有两个函数,一个用于解析html字符串以将其标题转换为数组



literal-conversion




和另一个将这些标题解析为目录功能列表



const str = "<h1>test-1<h1><h2>test1-1<h2><h3>test1-1-1</h3><h1>test1-2<h1><h2>test1-2-1</h2><h3>test1-2-2</h3><h1>test-2</h1><h1>test-3</h1><h1>test-4</h1>
"
const wrapper = document.createElement('div');
wrapper.innerHTML = str.trim();

let tree = [];
let leaf = null;

for (const node of wrapper.querySelectorAll("h1, h2, h3, h4, h5, h6"))
{
    const nodeLevel = parseInt(node.tagName[1]);
    const newLeaf = { level: nodeLevel, text: node.textContent, children: [], parent: leaf };

    while (leaf && newLeaf.level <= leaf.level)
        leaf = leaf.parent;

    if (!leaf)
        tree.push(newLeaf);
    else
        leaf.children.push(newLeaf);

    leaf = newLeaf;
}
&#13;
&#13;
&#13;

它输出一个像这样的字符串

const ol = document.createElement("ol");

(function makeOl(ol, leaves)
{
    for (const leaf of leaves)
    {
        const li = document.createElement("li");
        li.appendChild(new Text(leaf.text));

        if (leaf.children.length > 0)
        {
            const subOl = document.createElement("ol");
            makeOl(subOl, leaf.children);
            li.appendChild(subOl);
        }

        ol.appendChild(li);
    }
})(ol, tree);

呈现类似

的内容
  1. 试验-1
    1. test1-1
      1. test1-1-1
    2. test1-2
      1. test1-2-1
      2. test1-2-2
  2. 试验-2
  3. 测试-3
  4. 测试-4

我仍然习惯了React的jsx部分,我想知道如何转换该函数,以便ol和ls是React / jsx元素而不是一串原始html,因为它需要另一个步骤来渲染,例如。

"<ol><li>test-1<ol><li>test1-1<ol><li>test1-1-1</li></ol></li><li>test1-2<ol><li>test1-2-1</li><li>test1-2-2</li></ol></li></ol></li><li>test-2</li><li>test-3</li><li>test-4</li></ol>"

我使用jsx和数组的方式是这样的

<div dangerouslySetInnerHTML={{__html: olString}} />

2 个答案:

答案 0 :(得分:1)

您始终可以使用React.createElement

e.g。

React.createElement('div', null, `Hello ${this.props.toWhat}`);

但是,最佳实践可能是这样的。

&#13;
&#13;
// reusable Tree component
export default class Tree extends Component {

  static propTypes = {
    children: PropTypes.array.isRequired
  }

  render() {

    const { children } = this.props

    return (
      <ol>
        {children.map(leaf =>
          <li key={leaf.id}>
            <span>{leaf.text}</span>
            {leaf.children && <Tree children={leaf.children}/>}
          </li>
        )}
      </ol>
    )
  }
}

// (re)use it 
function render() {
  return (
    <Tree children={ tree } />
  );
}
&#13;
&#13;
&#13;

您甚至可以制作HTML Elements变量。

<Tree children={ tree } listNode="ul" listElementNode="li" />

然后在树组件

function render() {
    const {listNode: UL, listElementNode: LI} = this.props;
    return (<UL></UL>);
}

答案 1 :(得分:1)

目前,您有一个递归函数(makeOl),我将其替换为renderLeaf函数:

呈现这种方式的方法是:

class Tree extends React.Component {
  render() {
    let leafs = this.props.children
    return (
      <React.Fragment>
        {leafs.map(this.renderLeaf)}
      </React.Fragment>
    )
  }
  renderLeaf(leaf) {
    return (
      <ol>
        <li>
          {leaf.text}
          {leaf.children && leaf.children.map(this.renderLeaf)}
        </li>
      </ol>
    )
  }
}

然后您可以将其用作:<Tree children={tree} />