-
Notifications
You must be signed in to change notification settings - Fork 7.7k
react-markup
reference docs
#7107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
eps1lon
wants to merge
3
commits into
main
Choose a base branch
from
react-markup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
title: React Markup APIs | ||
--- | ||
|
||
<Intro> | ||
|
||
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. | ||
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. | ||
|
||
</Intro> | ||
|
||
## APIs {/*apis*/} | ||
|
||
These APIs can be imported from the React Server environment (e.g. in Server Actions): | ||
|
||
* [`renderToHTML`](/reference/react-markup/renderToHTML) renders a non-interactive React tree with support for Server Components but not Client Components. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
title: renderToHTML | ||
canary: true | ||
--- | ||
|
||
|
||
<Intro> | ||
|
||
`renderToHTML` renders a React tree to non-interactive HTML. | ||
|
||
```js | ||
const stream = renderToHTML(reactNode, options?) | ||
``` | ||
|
||
</Intro> | ||
|
||
<InlineToc /> | ||
|
||
--- | ||
|
||
## Reference {/*reference*/} | ||
|
||
### `renderToHTML(reactNode, options?)` {/*rendertohtml*/} | ||
|
||
You can call `renderToHTML` to render a non-interactive tree of React components to HTML. | ||
By default, it supports shared components and built-in components. | ||
Server Components are only allowed if used in a React Server environment (e.g. in Server Actions). | ||
You can also use it during Client-Side Rendering but only without Server Components. | ||
|
||
When a `<html>` tag is rendered, `renderToHTML` will automatically add `<!DOCTYPE html>` doctype. | ||
|
||
```js | ||
import { experimental_renderToHTML as renderToHTML } from 'react-markup'; | ||
|
||
const markup = await renderToHTML(<App />); | ||
``` | ||
|
||
#### Parameters {/*parameters*/} | ||
|
||
* `reactNode`: A React node you want to render to HTML. For example, a JSX element like `<App />`. | ||
|
||
* **optional** `options`: An object for server render. | ||
* **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. | ||
* **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. | ||
* **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. | ||
|
||
#### Returns {/*returns*/} | ||
|
||
`renderToHTML` returns a Promise that will resolve with the HTML string of the rendered React tree. | ||
|
||
#### Caveats {/*caveats*/} | ||
|
||
* Will throw when Client APIs (e.g. `useState` or `useEffect`) are used. | ||
|
||
--- | ||
|
||
## Usage {/*usage*/} | ||
|
||
### Rendering a React tree as an Email {/*rendering-a-react-tree-as-an-email*/} | ||
|
||
Await the call of `renderToHTML` : | ||
|
||
```js {7} | ||
import { experimental_renderToHTML as renderToHTML } from 'react-markup'; | ||
import EmailTemplate from './my-email-template-component.js' | ||
|
||
async function action(email, name) { | ||
"use server"; | ||
// ... in your server, e.g. a Server Action... | ||
const htmlString = await renderToHTML(<EmailTemplate name={name} />); | ||
// ... send e-mail using some e-mail provider | ||
await sendEmail({ to: email, contentType: 'text/html', body: htmlString }); | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.