You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/content/reference/react-dom/hydrate.md
+46-46
Original file line number
Diff line number
Diff line change
@@ -4,15 +4,15 @@ title: hydrate
4
4
5
5
<Deprecated>
6
6
7
-
This API will be removed in a future major version of React.
7
+
此 API 将在未来的 React 主要版本中被移除。
8
8
9
-
In React 18, `hydrate`was replaced by [`hydrateRoot`.](/reference/react-dom/client/hydrateRoot)Using `hydrate` in React 18 will warn that your app will behave as if it’s running React 17. Learn more [here.](/blog/2022/03/08/react-18-upgrade-guide#updates-to-client-rendering-apis)
`hydrate`lets you display React components inside a browser DOM node whose HTML content was previously generated by [`react-dom/server`](/reference/react-dom/server)in React 17 and below.
15
+
`hydrate`允许你在 React 17 及以下版本中将使用 [`react-dom/server`](/reference/react-dom/server)生成的 HTML 内容作为浏览器 DOM 节点,并在其中显示 React 组件。
React will attach to the HTML that exists inside the `domNode`, and take over managing the DOM inside it. An app fully built with React will usually only have one `hydrate`call with its root component.
39
+
React 将会附加到 `domNode` 内部现有的 HTML,并接管有关的 DOM 的管理。使用 React 完全构建的应用通常只会有一个 `hydrate`调用,并用于根组件。
40
40
41
-
[See more examples below.](#usage)
41
+
[参见下面更多示例](#usage)。
42
42
43
-
#### Parameters {/*parameters*/}
43
+
#### 参数 {/*parameters*/}
44
44
45
-
* `reactNode`: The "React node" used to render the existing HTML. This will usually be a piece of JSX like `<App />`which was rendered with a `ReactDOM Server` method such as `renderToString(<App />)`in React 17.
* `hydrate`expects the rendered content to be identical with the server-rendered content. React can patch up differences in text content, but you should treat mismatches as bugs and fix them.
57
-
* In development mode, React warns about mismatches during hydration. There are no guarantees that attribute differences will be patched up in case of mismatches. This is important for performance reasons because in most apps, mismatches are rare, and so validating all markup would be prohibitively expensive.
58
-
* You'll likely have only one `hydrate`call in your app. If you use a framework, it might do this call for you.
59
-
* If your app is client-rendered with no HTML rendered already, using `hydrate()` is not supported. Use [render()](/reference/react-dom/render) (for React 17 and below) or [createRoot()](/reference/react-dom/client/createRoot) (for React 18+) instead.
Using`hydrate()`to render a client-only app (an app without server-rendered HTML) is not supported. Use [`render()`](/reference/react-dom/render) (in React 17 and below) or [`createRoot()`](/reference/react-dom/client/createRoot) (in React 18+) instead.
### Hydrating server-rendered HTML {/*hydrating-server-rendered-html*/}
75
+
### hydrate 服务器渲染的 HTML {/*hydrating-server-rendered-html*/}
76
76
77
-
In React, "hydration" is how React "attaches" to existing HTML that was already rendered by React in a server environment. During hydration, React will attempt to attach event listeners to the existing markup and take over rendering the app on the client.
Usually you shouldn't need to call `hydrate` again or to call it in more places. From this point on, React will be managing the DOM of your application. To update the UI, your components will [use state.](/reference/react/useState)
If a single element’s attribute or text content is unavoidably different between the server and the client (for example, a timestamp), you may silence the hydration mismatch warning.
This only works one level deep, and is intended to be an escape hatch. Don’t overuse it. Unless it’s text content, React still won’t attempt to patch it up, so it may remain inconsistent until future updates.
If you intentionally need to render something different on the server and the client, you can do a two-pass rendering. Components that render something different on the client can read a [state variable](/reference/react/useState) like `isClient`, which you can set to `true` in an [effect](/reference/react/useEffect):
@@ -184,18 +184,18 @@ export default function App() {
184
184
185
185
return (
186
186
<h1>
187
-
{isClient ?'Is Client':'Is Server'}
187
+
{isClient ?'在客户端':'在服务器'}
188
188
</h1>
189
189
);
190
190
}
191
191
```
192
192
193
193
</Sandpack>
194
194
195
-
This way the initial render pass will render the same content as the server, avoiding mismatches, but an additional pass will happen synchronously right after hydration.
This approach makes hydration slower because your components have to render twice. Be mindful of the user experience on slow connections. The JavaScript code may load significantly later than the initial HTML render, so rendering a different UI immediately after hydration may feel jarring to the user.
199
+
这种方法会使 hydrate 变慢,因为你的组件必须渲染两次,因此要注意在网络不好情况下的用户体验。JavaScript 代码的加载可能比初始 HTML 渲染要晚许多,因此在 hydrate 后立即渲染不同的 UI 可能会让用户感到不适。
0 commit comments