|
| 1 | +--- |
| 2 | +title: renderToMarkup |
| 3 | +--- |
| 4 | + |
| 5 | + |
| 6 | +<Intro> |
| 7 | + |
| 8 | +`renderToMarkup` renders a React tree to non-interactive HTML. |
| 9 | + |
| 10 | +```js |
| 11 | +const stream = renderToMarkup(reactNode, options?) |
| 12 | +``` |
| 13 | +
|
| 14 | +</Intro> |
| 15 | +
|
| 16 | +<InlineToc /> |
| 17 | +
|
| 18 | +--- |
| 19 | +
|
| 20 | +## Reference {/*reference*/} |
| 21 | +
|
| 22 | +### `renderToMarkup(reactNode, options?)` {/*renderToMarkup*/} |
| 23 | +
|
| 24 | +You can call `renderToMarkup` in a React Server environment (e.g. in a Server Action) to render a non-interactive tree of React components to HTML. |
| 25 | +
|
| 26 | +```js |
| 27 | +import { renderToMarkup } from 'react-dom/server'; |
| 28 | + |
| 29 | +const markup = await renderToMarkup(<App />); |
| 30 | +``` |
| 31 | +
|
| 32 | +#### Parameters {/*parameters*/} |
| 33 | +
|
| 34 | +* `reactNode`: A React node you want to render to HTML. For example, a JSX element like `<App />`. |
| 35 | +
|
| 36 | +* **optional** `options`: An object for server render. |
| 37 | + * **optional** `identifierPrefix`: A string prefix React uses for IDs generated by [`useId`.](/reference/react/useId) Useful to avoid conflicts when the markup is embedded in or combined with other markup that is rendered by React. |
| 38 | + * **optional** `onError`: A callback that fires whenever there is a server error. By default, this only calls `console.error`. If you override it to [log crash reports,](#logging-crashes-on-the-server) make sure that you still call `console.error`. This is different to the rejection reason of the created Promise since it'll include the parent component stack. |
| 39 | + * **optional** `signal`: An [abort signal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) that lets you [abort rendering](#aborting-server-rendering). The original Promise will reject. |
| 40 | +
|
| 41 | +#### Returns {/*returns*/} |
| 42 | +
|
| 43 | +`renderToMarkup` returns a Promise that will resolve with the HTML string of the rendered React tree. |
| 44 | +
|
| 45 | +#### Caveats {/*caveats*/} |
| 46 | +
|
| 47 | +* Will throw when Client Components (e.g. `useState` or `useEffect`) are used. |
| 48 | +
|
| 49 | +--- |
| 50 | +
|
| 51 | +## Usage {/*usage*/} |
| 52 | +
|
| 53 | +### Rendering a React tree as an Email {/*rendering-a-react-tree-as-an-email*/} |
| 54 | +
|
| 55 | +Await the call of `renderToMarkup` : |
| 56 | +
|
| 57 | +```js {7} |
| 58 | +import { renderToMarkup } from 'react-markup'; |
| 59 | +import EmailTemplate from './my-email-template-component.js' |
| 60 | + |
| 61 | +async function action(email, name) { |
| 62 | + "use server"; |
| 63 | + // ... in your server, e.g. a Server Action... |
| 64 | + const htmlString = await renderToMarkup(<EmailTemplate name={name} />); |
| 65 | + // ... send e-mail using some e-mail provider |
| 66 | + await sendEmail({ to: email, contentType: 'text/html', body: htmlString }); |
| 67 | +} |
| 68 | +``` |
0 commit comments