简体   繁体   English

如何将 React JSX 材质 ui 元素转换为字符串

[英]How to convert React JSX material ui elements to string

I need to have my jsx in a string (as I am building a dynamic multi tab tab strip), so it could have 1 or 2 or 3 or 4 tabs.我需要将我的 jsx 放在一个字符串中(因为我正在构建一个动态的多标签标签条),所以它可以有 1 个或 2 个或 3 个或 4 个标签。

I can't use JSX because the JSX requires that elements be closed and in this case, the closing tags are only applied after the dynamic markup is created.我不能使用 JSX,因为 JSX 要求关闭元素,在这种情况下,关闭标签仅在创建动态标记后应用。

I have tried pushing the JSX into an array, but that also creates compile errors and following functions are then not seen.我曾尝试将 JSX 推入一个数组,但这也会产生编译错误,然后看不到以下函数。 (compiler wants to see the end Tags for the begin tags) (编译器希望看到开始标签的结束标签)

I have this:我有这个:

<div className={'page-content'} >
    <GenericsTabs
        value={selectedTab}
        onChange={handleTabChange}
    >
        <GenericsTab
            label={t("Language")}
            className={selectedTab === 0}
        />
        <GenericsTab
            label={t("Device")}
            className={selectedTab === 1}
        />
    </GenericsTabs>

    {tabPanelMarkup(0, 'Markup A')}

    {tabPanelMarkup(1, 'Markup B')}
</div>

I have also tried this: (building it as a string, but seems not getting the JSX material ui components recognised for what they are (after which I try using import parse from 'html-react-parser'; but the markup gets truncated or has pieces missing and the JSX components don't render what they should.我也试过这个:(将它构建为一个字符串,但似乎没有让 JSX 材料 ui 组件被识别为它们是什么(之后我尝试使用 import parse from 'html-react-parser';但标记被截断或缺少部分,JSX 组件没有呈现它们应该呈现的内容。

 const createTabStrip = (tabIndex) =>
 {
  let x = "<div className={page-content} >"
       x=x + " <MuiThemeProvider theme={kbTheme}>"
       x=x + " <tabStrip "
       x=x + "    value={ selectedTab }"
       x=x + "    onChange={handleTabChange}"
       x=x + "  >"

       for (let i=0; i< tabNames.length; i++)
       {
           x= x+ createTab(i)
       }

      for (let i=0; i< tabNames.length; i++)
      {
         x= x + createTabPanel(i)
      }
       
       x= x + closeTabStrip
       return x
  }

If I can just get the 'x=x+ ' bit working, the rest will follow.如果我能让 'x=x+ ' 位工作,rest 将随之而来。

Alternatively, I have tried this but no success, as the pushes after the first push, are not recognised for being pushes (they are grey instead of orange (using IntelliJ IDEA as the editor).或者,我尝试过但没有成功,因为第一次推送后的推送不会被识别为推送(它们是灰色而不是橙色(使用 IntelliJ IDEA 作为编辑器)。

Snip to illustrate:截图说明:

tabComponentJSx.push( <><div className={'page-content'} > )

在此处输入图像描述

let tabComponentJSx = [];
const createTabStrip = () =>
{
     let idx = tabNames.length;

     tabComponentJSx.push( <><div className={'page-content'} > )
     tabComponentJSx.push( <MuiThemeProvider theme={kbTheme} >)
     tabComponentJSx.push( <TabStrip value={selectedTab} onChange={handleTabChange} > </TabStrip> )
             
     // Create the tabPanels with the markup for each

     createTab()
     createTab()
             
     {tabPanelMarkup(0, 'Markup A')}
     {tabPanelMarkup(1, 'Markup B')}

     tabComponentJSx.push( </MuiThemeProvider> )
     tabComponentJSx.push( </div> </>)
}

Thx谢谢

// Builds the tab with it's name from the props.tabNames array
const createTab = () => {
    let x= []
    for (let i=0; i < tabNames.length; i++)
    {
        x.push( <EachTab label={ t( tabNames[i])} className={selectedTab ===0} /> )
    }
    return x;
}

// Builds each tab's Panel/Content  with the corresponding input elements from the 'tabsMarkup' array
const tabPanelMarkup = () => {
    let tabPanelJSX = [];

    for(let idx = 0; idx < tabNames.length; idx++)
    {
        tabPanelJSX.push(
              <TabPanel value={selectedTab} index={idx}>
                    <section className='no-padding-top margin-bottom-sm margin-top-sm'>
                        <div className={'container-fluid'}>
                            <div className='row'>
                                <div className='col-lg-12'>
                                    <div className='block'>
                                        <div className='row'>
                                            <div className='col-lg-12'>
                                             { tabsMarkup[idx] }
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                </TabPanel>
        )
    }

    return tabPanelJSX
}

let tabComponentJSx = [];
const createTabStrip = () =>
{
     tabComponentJSx.push(<div className={'page-content'} style={{ paddingTop:'0px'}}>
                              <MuiThemeProvider theme={kbTheme} >
                                  <GenericsTabs value={selectedTab} onChange={handleTabChange} >

                                     { createTab() }

                                  </GenericsTabs>

                                  {  tabPanelMarkup() }

                              </MuiThemeProvider>
                          </div>
                         )
};


createTabStrip()
return ( tabComponentJSx )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM