Skip to content

Commit b1c4b4e

Browse files
2239559319eps1lon
andauthored
useId add server rendering usage and server api add options (#6457)
Co-authored-by: Sebastian Silbermann <[email protected]>
1 parent 943e3ce commit b1c4b4e

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

src/content/reference/react-dom/server/renderToNodeStream.md

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ On the client, call [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) to
4343

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

46+
* **optional** `options`: An object for server render.
47+
* **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)
48+
4649
#### Returns {/*returns*/}
4750

4851
A [Node.js Readable Stream](https://nodejs.org/api/stream.html#readable-streams) that outputs an HTML string.

src/content/reference/react-dom/server/renderToStaticMarkup.md

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ It will produce non-interactive HTML output of your React components.
3535
#### Parameters {/*parameters*/}
3636

3737
* `reactNode`: A React node you want to render to HTML. For example, a JSX node like `<Page />`.
38+
* **optional** `options`: An object for server render.
39+
* **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.
3840

3941
#### Returns {/*returns*/}
4042

src/content/reference/react-dom/server/renderToStaticNodeStream.md

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

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

40+
* **optional** `options`: An object for server render.
41+
* **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.
42+
4043
#### Returns {/*returns*/}
4144

4245
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.

src/content/reference/react-dom/server/renderToString.md

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ On the client, call [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) to
4242

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

45+
* **optional** `options`: An object for server render.
46+
* **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)
47+
4548
#### Returns {/*returns*/}
4649

4750
An HTML string.

src/content/reference/react/useId.md

+37
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,40 @@ input { margin: 5px; }
302302
```
303303
304304
</Sandpack>
305+
306+
### useId in server rendering {/*useid-in-server-rendering*/}
307+
308+
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.
309+
310+
```js
311+
import { useId } from 'react';
312+
313+
function App() {
314+
const id = useId();
315+
// ...
316+
317+
```
318+
319+
```js
320+
/**
321+
* server side
322+
*/
323+
324+
import ReactServer from 'react-dom/server';
325+
326+
const { pipe } = ReactServer.renderToPipeableStream(<App />, { identifierPrefix: 'react-app1' });
327+
// ...
328+
329+
```
330+
331+
```js
332+
/**
333+
* client side
334+
*/
335+
336+
import { hydrateRoot } from 'react-dom/client';
337+
338+
const domNode = document.getElementById('root');
339+
const root = hydrateRoot(domNode, reactNode, { identifierPrefix: 'react-app1' });
340+
341+
```

0 commit comments

Comments
 (0)