Skip to content

Commit

Permalink
docs(cn): translate reference/react-dom/hydrate into Chinese (#1211)
Browse files Browse the repository at this point in the history
Co-authored-by: Xleine <919143384@qq.com>
  • Loading branch information
Yucohny and Xleine authored Jun 27, 2023
1 parent 414a40b commit 6598a82
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions src/content/reference/react-dom/hydrate.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ title: hydrate

<Deprecated>

This API will be removed in a future major version of React.
API 将在未来的 React 主要版本中被移除。

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)
React 18 开始,`hydrate` [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) 替代。在 React 18 或更高版本中使用 `hydrate` 将会警告你的应用程序行为会和 React 17 一样。如果你想了解更多,请看 [这里](/blog/2022/03/08/react-18-upgrade-guide#updates-to-client-rendering-apis)

</Deprecated>

<Intro>

`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.
`hydrate` 允许你在 React 17 及以下版本中将使用 [`react-dom/server`](/reference/react-dom/server) 生成的 HTML 内容作为浏览器 DOM 节点,并在其中显示 React 组件。

```js
hydrate(reactNode, domNode, callback?)
Expand All @@ -24,68 +24,68 @@ hydrate(reactNode, domNode, callback?)
---
## Reference {/*reference*/}
## 参考 {/*reference*/}
### `hydrate(reactNode, domNode, callback?)` {/*hydrate*/}
Call `hydrate` in React 17 and below to “attach” React to existing HTML that was already rendered by React in a server environment.
React 17 及以下版本中调用 `hydrate`,可以将 React “附加”到在服务器环境中已经由 React 渲染的现有 HTML 上。
```js
import { hydrate } from 'react-dom';

hydrate(reactNode, domNode);
```
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.
React 将会附加到 `domNode` 内部现有的 HTML,并接管有关的 DOM 的管理。使用 React 完全构建的应用通常只会有一个 `hydrate` 调用,并用于根组件。
[See more examples below.](#usage)
[参见下面更多示例](#usage)
#### Parameters {/*parameters*/}
#### 参数 {/*parameters*/}
* `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.
* `reactNode`:此参数用于渲染现有的 HTML。这通常是像 `<App />` 这样的 JSX 片段,并且在 React 17 中使用如 `renderToString(<App />)` `ReactDOM Server` 方法进行渲染。
* `domNode`: A [DOM element](https://developer.mozilla.org/en-US/docs/Web/API/Element) that was rendered as the root element on the server.
* `domNode`:在服务器中被渲染为根节点的 [DOM 元素](https://developer.mozilla.org/en-US/docs/Web/API/Element)
* **optional**: `callback`: A function. If passed, React will call it after your component is hydrated.
* **可选属性** `callback`:一个函数。如果传递了该参数,React 将会在组件 hydrate 后调用它。
#### Returns {/*returns*/}
#### 返回值 {/*returns*/}
`hydrate` returns null.
`hydrate` 返回 `null`
#### Caveats {/*caveats*/}
* `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.
* 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.
* You'll likely have only one `hydrate` call in your app. If you use a framework, it might do this call for you.
* 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.
#### 注意 {/*caveats*/}
* `hydrate` 要求渲染的内容与服务器渲染的内容完全相同。尽管 React 可以修复文本内容的差异,但你应该首先将不匹配视为错误并进行修复。
* 在开发模式下,React 会在 hydration 期间警告不匹配的错误。如果存在不匹配情况,无法保证属性的差异会被修补。在大多数应用程序中不匹配是很少见的,所以验证所有标记的代价将会很高。因此考虑到性能原因,这是很重要的。
* 你的应用程序中可能只有一个 `hydrate` 调用。如果你使用了框架,它可能会为你执行此调用。
* 如果你的应用程序是客户端渲染的,并且没有已经渲染的 HTML,则不支持使用 `hydrate()`。请改用 [render()](/reference/react-dom/render)(适用于 React 17 及以下版本)或 [createRoot()](/reference/react-dom/client/createRoot)(适用于 React 18 及以上版本)。
---
## Usage {/*usage*/}
## 用法 {/*usage*/}
Call `hydrate` to attach a <CodeStep step={1}>React component</CodeStep> into a server-rendered <CodeStep step={2}>browser DOM node</CodeStep>.
调用 `hydrate` <CodeStep step={1}>React 组件</CodeStep> 附加(attach)到服务器渲染的 <CodeStep step={2}>浏览器 DOM 节点</CodeStep>
```js [[1, 3, "<App />"], [2, 3, "document.getElementById('root')"]]
import { hydrate } from 'react-dom';

hydrate(<App />, document.getElementById('root'));
```
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.
不支持使用 `hydrate()` 渲染仅用于客户端的应用程序(没有服务器渲染的 HTML)。请改用 [`render()`](/reference/react-dom/render)(适用于 React 17 及以下版本)或 [`createRoot()`](/reference/react-dom/client/createRoot)(适用于 React 18 及以上版本)。
### Hydrating server-rendered HTML {/*hydrating-server-rendered-html*/}
### hydrate 服务器渲染的 HTML {/*hydrating-server-rendered-html*/}
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.
React 中,hydrate 是指将 React “附加(attach)”到在服务器环境中已由 React 渲染的现有 HTML 上。在 hydrate 期间,React 将尝试将事件监听器附加(attach)到现有标记,并在客户端上接管渲染应用程序。
In apps fully built with React, **you will usually only hydrate one "root", once at startup for your entire app**.
在完全使用 React 构建的应用程序中,**通常只会在第一次启动整个应用程序时,hydrate “根”节点**。
<Sandpack>
```html public/index.html
<!--
HTML content inside <div id="root">...</div>
was generated from App by react-dom/server.
`<div id="root">...</div>` 中的 HTML 内容是
`react-dom/server``App` 生成的。
-->
<div id="root"><h1>Hello, world!</h1></div>
<div id="root"><h1>你好,世界!</h1></div>
```
```js index.js active
Expand All @@ -98,32 +98,32 @@ hydrate(<App />, document.getElementById('root'));
```js App.js
export default function App() {
return <h1>Hello, world!</h1>;
return <h1>你好,世界!</h1>;
}
```
</Sandpack>
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)
通常情况下,你不需要再次调用 `hydrate`,也不需要在更多地方调用它。从此时开始,React 将会管理你的应用程序的 DOM。为了更新 UI,你的组件将会 [使用 state](/reference/react/useState)
For more information on hydration, see the docs for [`hydrateRoot`.](/reference/react-dom/client/hydrateRoot)
有关 hydrate 的更多信息,请参阅 [`hydrateRoot`](/reference/react-dom/client/hydrateRoot)
---
### Suppressing unavoidable hydration mismatch errors {/*suppressing-unavoidable-hydration-mismatch-errors*/}
### 抑制不可避免的 hydrate 不匹配错误 {/*suppressing-unavoidable-hydration-mismatch-errors*/}
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.
如果服务器和客户端之间某个元素的属性或文本内容无法避免不同(比如一个时间戳),你可以禁止 hydrate 警告。
To silence hydration warnings on an element, add `suppressHydrationWarning={true}`:
使用 `suppressHydrationWarning={true}` 禁止 hydrate 警告:
<Sandpack>
```html public/index.html
<!--
HTML content inside <div id="root">...</div>
was generated from App by react-dom/server.
`<div id="root">...</div>` 中的 HTML 内容是
`react-dom/server``App` 生成的。
-->
<div id="root"><h1>Current Date: 01/01/2020</h1></div>
<div id="root"><h1>当前时间:01/01/2020</h1></div>
```
```js index.js
Expand All @@ -138,30 +138,30 @@ hydrate(<App />, document.getElementById('root'));
export default function App() {
return (
<h1 suppressHydrationWarning={true}>
Current Date: {new Date().toLocaleDateString()}
当前时间:{new Date().toLocaleDateString()}
</h1>
);
}
```
</Sandpack>
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.
这只在同级深度上有效,而且是一种应急方法,因此不要过度使用它。除非它是文本内容,否则 React 仍然不会尝试修补它,因此直至未来的更新它都可能会保持不一致。
---
### Handling different client and server content {/*handling-different-client-and-server-content*/}
### 处理不同的客户端和服务器内容 {/*handling-different-client-and-server-content*/}
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):
如果需要在服务器和客户端上故意渲染不同的内容,可以进行双重渲染。在客户端上渲染不同内容的组件可以读取像 `isClient` 这样的 [state 变量](/reference/react/useState),你可以在 [effect](/reference/react/useEffect) 中将其设置为 `true`
<Sandpack>
```html public/index.html
<!--
HTML content inside <div id="root">...</div>
was generated from App by react-dom/server.
`<div id="root">...</div>` 中的 HTML 内容是
`react-dom/server``App` 生成的。
-->
<div id="root"><h1>Is Server</h1></div>
<div id="root"><h1>在服务器</h1></div>
```
```js index.js
Expand All @@ -184,18 +184,18 @@ export default function App() {

return (
<h1>
{isClient ? 'Is Client' : 'Is Server'}
{isClient ? '在客户端' : '在服务器'}
</h1>
);
}
```
</Sandpack>
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.
这样,初始渲染过程将呈现与服务器相同的内容,并且避免不匹配的情况,但会在 hydrate 后立即同步并进行额外的渲染。
<Pitfall>
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.
这种方法会使 hydrate 变慢,因为你的组件必须渲染两次,因此要注意在网络不好情况下的用户体验。JavaScript 代码的加载可能比初始 HTML 渲染要晚许多,因此在 hydrate 后立即渲染不同的 UI 可能会让用户感到不适。
</Pitfall>

0 comments on commit 6598a82

Please sign in to comment.