将JSX对象转换为HTML [英] Convert react JSX object to HTML

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

问题描述

我有一个react应用程序,它根据某些配置生成HTML输出。像这样:

I have a react application that generates HTML output based on some configuration. Like this:

export const getHtml = (config) => {
  const {classes, children} = config
  return (<div className={classes.join(' ')}>{children}</div>);
}

在反应应用中,我可以轻松显示生成的DOM对象,但我想要将HTML代码保存到DB,将其显示在不同的页面上(无需再次加载反应/解析配置)

Inside the react app I can easily display the resulting DOM objects, but I want to save the HTML code to DB, to display it on a different page (without loading react/parsing the config again)

我找不到转换JSX的方法对象为纯HTML ...

I could not find a way to convert the JSX object to plain HTML...

推荐答案

使用 renderToStaticMarkup 方法 - 它生成HTML字符串,我们可以快速传输到线路:

Use renderToStaticMarkup method - it produces HTML strings that we can transfer to the wire quickly:

const htmlString = ReactDOMServer.renderToStaticMarkup(
      <div ...
);

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

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