Convert a React.element to a JSX string

If you want the HTML representation of a React element you can use #renderToString or #renderToStaticMarkup.

import ReactDomServer from 'react-dom/server';

ReactDomServer.renderToString(<div>p</div>);
ReactDomServer.renderToStaticMarkup(<div>p</div>);

will produce

<div data-reactid=".1" data-react-checksum="-1666119235">p</div>

and

<div>p</div>

respectively. You will however not be able to render the parent as a string from within itself. If you need the parent too then from where you render the parent pass a renderToString version of it as props to itself.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)