Skip to content
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

Sync with react.dev @ bbb08a5a #31

Merged
merged 17 commits into from
Apr 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
useId add server rendering usage and server api add options (#6457)
Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
  • Loading branch information
2239559319 and eps1lon authored Dec 5, 2023
commit b1c4b4e6de827dbbe5e0f759f00b5f8edb7b800c
3 changes: 3 additions & 0 deletions src/content/reference/react-dom/server/renderToNodeStream.md
Original file line number Diff line number Diff line change
@@ -43,6 +43,9 @@ On the client, call [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) to

* `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 using multiple roots on the same page. Must be the same prefix as passed to [`hydrateRoot`.](/reference/react-dom/client/hydrateRoot#parameters)

#### Returns {/*returns*/}

A [Node.js Readable Stream](https://nodejs.org/api/stream.html#readable-streams) that outputs an HTML string.
Original file line number Diff line number Diff line change
@@ -35,6 +35,8 @@ It will produce non-interactive HTML output of your React components.
#### Parameters {/*parameters*/}

* `reactNode`: A React node you want to render to HTML. For example, a JSX node like `<Page />`.
* **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 using multiple roots on the same page.

#### Returns {/*returns*/}

Original file line number Diff line number Diff line change
@@ -37,6 +37,9 @@ The stream will produce non-interactive HTML output of your React components.

* `reactNode`: A React node you want to render to HTML. For example, a JSX element like `<Page />`.

* **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 using multiple roots on the same page.

#### Returns {/*returns*/}

A [Node.js Readable Stream](https://nodejs.org/api/stream.html#readable-streams) that outputs an HTML string. The resulting HTML can't be hydrated on the client.
3 changes: 3 additions & 0 deletions src/content/reference/react-dom/server/renderToString.md
Original file line number Diff line number Diff line change
@@ -42,6 +42,9 @@ On the client, call [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) to

* `reactNode`: A React node you want to render to HTML. For example, a JSX node 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 using multiple roots on the same page. Must be the same prefix as passed to [`hydrateRoot`.](/reference/react-dom/client/hydrateRoot#parameters)

#### Returns {/*returns*/}

An HTML string.
37 changes: 37 additions & 0 deletions src/content/reference/react/useId.md
Original file line number Diff line number Diff line change
@@ -302,3 +302,40 @@ input { margin: 5px; }
```

</Sandpack>

### useId in server rendering {/*useid-in-server-rendering*/}

Choose a unique id prefix and pass it into the server options and client options. `useId` will return the same id string on server side and client side. The following example selects `react-app1` as the id prefix.

```js
import { useId } from 'react';

function App() {
const id = useId();
// ...

```

```js
/**
* server side
*/

import ReactServer from 'react-dom/server';

const { pipe } = ReactServer.renderToPipeableStream(<App />, { identifierPrefix: 'react-app1' });
// ...

```

```js
/**
* client side
*/

import { hydrateRoot } from 'react-dom/client';

const domNode = document.getElementById('root');
const root = hydrateRoot(domNode, reactNode, { identifierPrefix: 'react-app1' });

```