Skip to content

Commit c0bd72d

Browse files
committed
react-markup reference
1 parent 2bfa7a6 commit c0bd72d

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: React Markup APIs
3+
---
4+
5+
<Intro>
6+
7+
The `react-markup` package provides the ability to render standalone HTML from Server Components for use in embedded contexts such as e-mails and RSS/Atom feeds.
8+
It cannot use Client Components and does not hydrate. It is intended to be paired with the generic React package, which is shipped as `react` to npm.
9+
10+
</Intro>
11+
12+
## APIs {/*apis*/}
13+
14+
These APIs can be imported from the React Server environment (e.g. in Server Actions):
15+
16+
* [`renderToMarkup`](/reference/react-markup/renderToMarkup) renders a non-interactive React tree with support for Server Components but not Client Components.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
```

src/sidebarReference.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,18 @@
390390
}
391391
]
392392
},
393+
{
394+
"title": "react-markup",
395+
"path": "/reference/react-markup",
396+
"canary": true,
397+
"routes": [
398+
{
399+
"title": "renderToMarkup",
400+
"path": "/reference/react-markup/renderToMarkup",
401+
"canary": true
402+
}
403+
]
404+
},
393405
{
394406
"hasSectionHeader": true,
395407
"sectionHeader": "Legacy APIs"
@@ -433,4 +445,4 @@
433445
]
434446
}
435447
]
436-
}
448+
}

0 commit comments

Comments
 (0)