Convert react JSX object to 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>);

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)

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



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

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

redux code:

Here is the full code that I had to use in my react/redux application.

import React from 'react';
import ReactDOMServer from 'react-dom/server';
import {Provider} from 'react-redux';


class ParentComponent extends React.Component {


    getHtml(config) {
        const {classes, children} = config
        return ReactDOMServer.renderToStaticMarkup(
            <Provider store={}>
                <ChildComponent classes={classes}>{children}</ChildComponent>

ParentComponent.contextTypes = { store: React.PropTypes.object };


  • Use ReactDOMServer.renderToStaticMarkup() to get the HTML code
  • Specify ParentComponent.contextTypes to make available
  • Need to wrap the ChildComponent in a Provider so it has access to the redux store.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.