将多级JSON菜单转换为多级JSX/HTML菜单 [英] Converting a multi-level JSON menu to a multi-level JSX/HTML menu

查看:93
本文介绍了将多级JSON菜单转换为多级JSX/HTML菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SmartMenus 创建下拉菜单.但是,我想动态创建菜单. React应用程序将向API服务器查询JSON代码,并从中构建一个菜单.我正在尝试找出一种将JSON代码转换为HTML/JSX代码的方法:

I am using SmartMenus to create a drop down menu. However, I am wanting to create the menu dynamically. The React app will query the API server for JSON code and a menu will be constructed out of that. I am trying to figure out a way to convert the JSON code to HTML/JSX code:

从API检索到的JSON代码如下所示:

The JSON code retrieved from the API will look something like this:

{
        "module_type": "menu",
        "title": "My Site",
        "menu": [
                {
                        "link": "/home",
                        "title": "Home"
                },
                {
                        "link": "#",
                        "title": "Fruit",
                        "menu": [
                                {
                                        "link": "/apples",
                                        "title": "Apples"
                                },
                                {
                                        "link": "/bananas",
                                        "title": "Bananas"
                                },
                                {
                                        "link": "/kiwi",
                                        "title": "Kiwi"
                                },
                                {
                                        "link": "/pears",
                                        "title": "Pears"
                                }
                        ]
                },
                {
                        "link": "#",
                        "title": "Vegetables",
                        "menu": [
                                {
                                        "link": "/carrots",
                                        "title": "Carrots"
                                },
                                {
                                        "link": "/celery",
                                        "title": "Celery"
                                },
                                {
                                        "link": "/potatoes",
                                        "title": "Potatoes"
                                },
                                {
                                        "link": "#",
                                        "title": "More",
                                        "menu": [
                                              {
                                                      "link": "/thirdlevel1",
                                                      "title": "3rd level menu"
                                              },
                                              {
                                                      "link": "/thirdlevel2",
                                                      "title": "3rd level two"
                                              }
                                        ]
                               }
                        ]
                },
                {
                        "link": "/about",
                        "title": "About"
                },
                {
                        "link": "/contact",
                        "title": "Contact"
                }
        ]
}

基于此JSON数据,我想生成以下HTML/JSX代码:

Based on this JSON data, I would like to generate the following HTML/JSX code:

<nav className="main-nav" role="navigation">

  <input id="main-menu-state" type="checkbox" />
  <label className="main-menu-btn" htmlFor="main-menu-state">
    <span className="main-menu-btn-icon"></span> Toggle main menu visibility
  </label>

  <h2 className="nav-brand"><a href="#">My Site</a></h2>


  <ul id="main-menu" className="sm sm-blue">
    <li className="nav-item"><Link to="/">Home</Link></li>
    <li><a href="#">No Fruit</a>
      <ul>
        <li><Link to="/apples">Apples</Link></li>
        <li><Link to="/bananas">Bananas</Link></li>
        <li><Link to="/kiwi">Kiwi</Link></li>
        <li><Link to="/pears">Pears</Link></li>
      </ul>
    </li>
    <li><a href="#">Vegetables</a>
      <ul>
        <li className="nav-item"><Link to="/carrots">Carrots</Link></li>
        <li className="nav-item"><Link to="/celery">Celery</Link></li>
        <li className="nav-item"><Link to="/potatoes">Potatoes</Link></li>
        <li><a href="#">more...</a>
          <ul>
            <li><a href="#>3rd level menu</a></li>
              <li><a href="#>3rd level two</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li className="nav-item"><Link to="/about">About</Link></li>
    <li className="nav-item"><Link to="/contact">Contact</Link></li>
  </ul>
</nav>

以下链接提供了一种解决方案:旋转嵌套将JSON转换为使用Javascript的HTML嵌套列表.但是,这似乎不适用于JSX,因为您不能将document.createElement()与React/JSX元素一起使用.

The following link offers a solution: Turning nested JSON into an HTML nested list with Javascript . However, this does not seem to work well with JSX as you cannot use document.createElement() with React/JSX elements.

鉴于我正在使用多个级别的菜单,在React中混合使用JSX和html元素的一种有效方法是什么?

Given that I am using multiple levels of menus, what is an efficient way to do this in React with a mix of JSX and html elements?

推荐答案

这是使用JSX和示例数据动态生成的菜单.

Here's a dynamically generated menu, using JSX and your sample data.

如您所见,我们在构建JSX时递归地遍历了您的菜单项:

As you can see, we're recursively iterating over your menu items while building the JSX:

const renderMenu = items => {
  return <ul>
    { items.map(i => {
      return <li>
        <a href={i.link}>{ i.title }</a>
        { i.menu && renderMenu(i.menu) }
      </li>
    })}
  </ul>
}

const Menu = ({ data }) => {
  return <nav>
    <h2>{ data.title }</h2>
    { renderMenu(data.menu) }
  </nav>
}

ReactDOM.render(
  <Menu data={data} />,
  document.getElementById('container')
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script>
  // The sample data is declared here, only to keep the React example short and clear
  const data = {
    "module_type": "menu",
    "title": "My Site",
    "menu": [{
        "link": "/home",
        "title": "Home"
      },
      {
        "link": "#",
        "title": "Fruit",
        "menu": [{
            "link": "/apples",
            "title": "Apples"
          },
          {
            "link": "/bananas",
            "title": "Bananas"
          },
          {
            "link": "/kiwi",
            "title": "Kiwi"
          },
          {
            "link": "/pears",
            "title": "Pears"
          }
        ]
      },
      {
        "link": "#",
        "title": "Vegetables",
        "menu": [{
            "link": "/carrots",
            "title": "Carrots"
          },
          {
            "link": "/celery",
            "title": "Celery"
          },
          {
            "link": "/potatoes",
            "title": "Potatoes"
          },
          {
            "link": "#",
            "title": "More",
            "menu": [{
                "link": "/thirdlevel1",
                "title": "3rd level menu"
              },
              {
                "link": "/thirdlevel2",
                "title": "3rd level two"
              }
            ]
          }
        ]
      },
      {
        "link": "/about",
        "title": "About"
      },
      {
        "link": "/contact",
        "title": "Contact"
      }
    ]
  }
</script>
<div id="container"></div>

这篇关于将多级JSON菜单转换为多级JSX/HTML菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆