Skip to content

Commit ff632b3

Browse files
GarlicGoYucohny
andauthored
docs(cn): translate reference/react-dom/server/renderToString into Ch… (#1181)
Co-authored-by: Yucohny <yucohny@163.com>
1 parent cad31ea commit ff632b3

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

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

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ title: renderToString
44

55
<Pitfall>
66

7-
`renderToString` does not support streaming or waiting for data. [See the alternatives.](#alternatives)
7+
`renderToString` 不支持流式传输或等待数据。[请参考替代方案](#alternatives)
88

99
</Pitfall>
1010

1111
<Intro>
1212

13-
`renderToString` renders a React tree to an HTML string.
13+
`renderToString` React 树渲染为一个 HTML 字符串。
1414

1515
```js
1616
const html = renderToString(reactNode)
@@ -26,89 +26,89 @@ const html = renderToString(reactNode)
2626

2727
### `renderToString(reactNode)` {/*rendertostring*/}
2828

29-
On the server, call `renderToString` to render your app to HTML.
29+
在服务器,调用 `renderToString` 将你的应用渲染为 HTML
3030

3131
```js
3232
import { renderToString } from 'react-dom/server';
3333

3434
const html = renderToString(<App />);
3535
```
3636

37-
On the client, call [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) to make the server-generated HTML interactive.
37+
在客户端,调用 [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) 来使服务器生成的 HTML 具有交互性。
3838

39-
[See more examples below.](#usage)
39+
[请参考下面的更多示例](#usage)
4040

4141
#### Parameters {/*parameters*/}
4242

43-
* `reactNode`: A React node you want to render to HTML. For example, a JSX node like `<App />`.
43+
* `reactNode`:你要渲染为 HTML 的 React 节点。例如,一个 JSX 节点,就像 `<App />`
4444

45-
#### Returns {/*returns*/}
45+
#### 返回值 {/*returns*/}
4646

47-
An HTML string.
47+
一个 HTML 字符串。
4848

49-
#### Caveats {/*caveats*/}
49+
#### 注意事项 {/*caveats*/}
5050

51-
* `renderToString` has limited Suspense support. If a component suspends, `renderToString` immediately sends its fallback as HTML.
51+
* `renderToString` Suspense 的支持有限。如果一个组件挂起(suspend),`renderToString` 会立即将其 fallback 作为 HTML 发送。
5252

53-
* `renderToString` works in the browser, but using it in the client code is [not recommended.](#removing-rendertostring-from-the-client-code)
53+
* `renderToString` 可以在浏览器中工作,但 [不推荐](#removing-rendertostring-from-the-client-code) 在客户端代码中使用它。
5454

5555
---
5656

57-
## Usage {/*usage*/}
57+
## 用法 {/*usage*/}
5858

59-
### Rendering a React tree as HTML to a string {/*rendering-a-react-tree-as-html-to-a-string*/}
59+
### React 树渲染为 HTML 字符串 {/*rendering-a-react-tree-as-html-to-a-string*/}
6060

61-
Call `renderToString` to render your app to an HTML string which you can send with your server response:
61+
调用 `renderToString` 将你的应用渲染为 HTML 字符串,你可以将其与服务器响应一起发送:
6262

6363
```js {5-6}
6464
import { renderToString } from 'react-dom/server';
6565

66-
// The route handler syntax depends on your backend framework
66+
// 路由处理程序的语法取决于你使用的后端框架
6767
app.use('/', (request, response) => {
6868
const html = renderToString(<App />);
6969
response.send(html);
7070
});
7171
```
7272

73-
This will produce the initial non-interactive HTML output of your React components. On the client, you will need to call [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) to *hydrate* that server-generated HTML and make it interactive.
73+
这将生成你的 React 组件的初始非交互式 HTML 输出。在客户端上,你需要调用 [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) 来将服务器生成的 HTML 进行 hydrate 处理,使其具有交互功能。
7474

7575

7676
<Pitfall>
7777

78-
`renderToString` does not support streaming or waiting for data. [See the alternatives.](#alternatives)
78+
`renderToString` 不支持流式传输或等待数据。[请参考替代方案](#alternatives)
7979

8080
</Pitfall>
8181

8282
---
8383

84-
## Alternatives {/*alternatives*/}
84+
## 替代方案 {/*alternatives*/}
8585

86-
### Migrating from `renderToString` to a streaming method on the server {/*migrating-from-rendertostring-to-a-streaming-method-on-the-server*/}
86+
### `renderToString` 迁移到服务器上的流式传输方法 {/*migrating-from-rendertostring-to-a-streaming-method-on-the-server*/}
8787

88-
`renderToString` returns a string immediately, so it does not support streaming or waiting for data.
88+
`renderToString` 立即返回一个字符串,因此不支持流式传输或等待数据。
8989

90-
When possible, we recommend using these fully-featured alternatives:
90+
如果可能的话,我们建议使用这些功能完整的替代方法:
9191

92-
* If you use Node.js, use [`renderToPipeableStream`.](/reference/react-dom/server/renderToPipeableStream)
93-
* If you use Deno or a modern edge runtime with [Web Streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API), use [`renderToReadableStream`.](/reference/react-dom/server/renderToReadableStream)
92+
* 如果你使用 Node.js,请使用 [`renderToPipeableStream`](/reference/react-dom/server/renderToPipeableStream)
93+
* 如果你使用 Deno 或支持 [Web Streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API) 的现代运行时,请使用 [`renderToReadableStream`](/reference/react-dom/server/renderToReadableStream)
9494

95-
You can continue using `renderToString` if your server environment does not support streams.
95+
如果你的服务器环境不支持流式传输,你仍然可以继续使用 `renderToString`
9696

9797
---
9898

99-
### Removing `renderToString` from the client code {/*removing-rendertostring-from-the-client-code*/}
99+
### 从客户端代码中移除 `renderToString` {/*removing-rendertostring-from-the-client-code*/}
100100

101-
Sometimes, `renderToString` is used on the client to convert some component to HTML.
101+
有时,`renderToString` 用于在客户端将某个组件转换为 HTML
102102

103103
```js {1-2}
104-
// 🚩 Unnecessary: using renderToString on the client
104+
// 🚩 不必要:在客户端使用 renderToString
105105
import { renderToString } from 'react-dom/server';
106106

107107
const html = renderToString(<MyIcon />);
108-
console.log(html); // For example, "<svg>...</svg>"
108+
console.log(html); // 例如,"<svg>...</svg>"
109109
```
110110

111-
Importing `react-dom/server` **on the client** unnecessarily increases your bundle size and should be avoided. If you need to render some component to HTML in the browser, use [`createRoot`](/reference/react-dom/client/createRoot) and read HTML from the DOM:
111+
在客户端导入 `react-dom/server` 会不必要地增加 bundle 大小,所以应该避免这样做。如果你需要在浏览器中将某个组件渲染为 HTML,请使用 [`createRoot`](/reference/react-dom/client/createRoot) 并从 DOM 中读取 HTML。
112112

113113
```js
114114
import { createRoot } from 'react-dom/client';
@@ -119,20 +119,20 @@ const root = createRoot(div);
119119
flushSync(() => {
120120
root.render(<MyIcon />);
121121
});
122-
console.log(div.innerHTML); // For example, "<svg>...</svg>"
122+
console.log(div.innerHTML); // 例如,"<svg>...</svg>"
123123
```
124124

125-
The [`flushSync`](/reference/react-dom/flushSync) call is necessary so that the DOM is updated before reading its [`innerHTML`](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML) property.
125+
[`flushSync`](/reference/react-dom/flushSync) 调用是必要的,以便在读取 [`innerHTML`](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML) 属性之前更新 DOM。
126126

127127
---
128128

129-
## Troubleshooting {/*troubleshooting*/}
129+
## 故障排除 {/*troubleshooting*/}
130130

131-
### When a component suspends, the HTML always contains a fallback {/*when-a-component-suspends-the-html-always-contains-a-fallback*/}
131+
### 当组件暂停时,HTML 中始终包含一个 fallback {/*when-a-component-suspends-the-html-always-contains-a-fallback*/}
132132

133-
`renderToString` does not fully support Suspense.
133+
`renderToString` 不完全支持 Suspense
134134

135-
If some component suspends (for example, because it's defined with [`lazy`](/reference/react/lazy) or fetches data), `renderToString` will not wait for its content to resolve. Instead, `renderToString` will find the closest [`<Suspense>`](/reference/react/Suspense) boundary above it and render its `fallback` prop in the HTML. The content will not appear until the client code loads.
135+
如果某个组件 suspend(例如,因为它使用 [`lazy`](/reference/react/lazy) 定义或获取数据),`renderToString` 不会等待其内容解析完成。相反,`renderToString` 将找到最近的 [`<Suspense>`](/reference/react/Suspense) 边界,并在 HTML 中渲染其 `fallback` 属性。直到客户端代码加载后,内容才会显示出来。
136136

137-
To solve this, use one of the [recommended streaming solutions.](#migrating-from-rendertostring-to-a-streaming-method-on-the-server) They can stream content in chunks as it resolves on the server so that the user sees the page being progressively filled in before the client code loads.
137+
要解决这个问题,请使用其中一个 [推荐的流式解决方案](#migrating-from-rendertostring-to-a-streaming-method-on-the-server)。它们可以在服务器上逐步以块的形式流式传输内容,使用户在客户端代码加载之前逐步看到页面填充。
138138

0 commit comments

Comments
 (0)