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

[Beta] docs(cn): translate render-and-commit into Chinese #686

Merged
merged 9 commits into from
Jan 19, 2022
110 changes: 56 additions & 54 deletions beta/src/pages/learn/render-and-commit.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
---
title: Render and Commit
title: 渲染和提交
translators:
- oceanlvr
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
---

<Intro>

Before your components are displayed on screen, they must be rendered by React. Understanding the steps in this process will help you think about how your code executes and explain its behavior.
组件显示到屏幕之前,其必须被 React 渲染。理解这些处理步骤将帮助您思考代码的执行过程并能解释其行为。

</Intro>

<YouWillLearn>

* What rendering means in React
* When and why React renders a component
* The steps involved in displaying a component on screen
* Why rendering does not always produce a DOM update
* React 中渲染的含义是什么
* 为什么以及什么时候 React 会渲染一个组件
* 在屏幕上显示组件所涉及的步骤
* 为什么渲染不总是伴随着 DOM 更新
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

</YouWillLearn>

Imagine that your components are cooks in the kitchen, assembling tasty dishes from ingredients. In this scenario, React is the waiter who puts in requests from customers and brings them their orders. This process of requesting and serving UI has three steps:
想象一下,您的组件是厨房里的厨师,用食材“组装”成美味的菜肴。在这种场景下,React 是一名服务员,他接收客户的请求并给用户们带回完成的订单。请求和提供 UI 的过程总共包括三个步骤:
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

1. **Triggering** a render (delivering the guest's order to the kitchen)
2. **Rendering** the component (getting the order from the kitchen)
3. **Committing** to the DOM (placing the order on the table)
1. 渲染 **触发中** (传递客户的订单到厨房)
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
2. 组件 **渲染中** (从厨房取得完成的订单)
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
3. 修改 **提交中** (将订单放在桌子上)
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

<IllustrationBlock sequential>
<Illustration caption="Trigger" alt="React as a server in a restaurant, fetching orders from the users and delivering them to the Component Kitchen." src="/images/docs/illustrations/i_render-and-commit1.png" />
<Illustration caption="Render" alt="The Card Chef gives React a fresh Card component." src="/images/docs/illustrations/i_render-and-commit2.png" />
<Illustration caption="Commit" alt="React delivers the Card to the user at their table." src="/images/docs/illustrations/i_render-and-commit3.png" />
<Illustration caption="触发" alt="React as a server in a restaurant, fetching orders from the users and delivering them to the Component Kitchen." src="/images/docs/illustrations/i_render-and-commit1.png" />
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
<Illustration caption="渲染" alt="The Card Chef gives React a fresh Card component." src="/images/docs/illustrations/i_render-and-commit2.png" />
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
<Illustration caption="提交" alt="React delivers the Card to the user at their table." src="/images/docs/illustrations/i_render-and-commit3.png" />
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
</IllustrationBlock>

## Step 1: Trigger a render {/*step-1-trigger-a-render*/}
## 步骤 1: 触发一次渲染 {/*step-1-trigger-a-render*/}

There are two reasons for a component to render:
有两种原因会导致组件的渲染:

1. It's the component's **initial render.**
2. The component's **state has been updated.**
1. 组件的 **初始化渲染。**
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
2. 组件的 **状态发生了改变。**

### Initial render {/*initial-render*/}
### 初始化渲染 {/*initial-render*/}
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

When your app starts, you need to trigger the initial render. Frameworks and sandboxes sometimes hide this code, but it's done by calling `ReactDOM.render` with your root component and the target DOM node:
当应用启动时,会触发初始渲染。框架和沙箱有时会隐藏这部分代码,但这是通过使用根组件和目标 DOM 节点调用 `ReactDOM.render` 来达成的:

<Sandpack>

Expand All @@ -65,36 +67,36 @@ export default function Image() {

</Sandpack>

Try commenting out the `ReactDOM.render` call and see the component disappear!
试着注释掉 `ReactDOM.render`,然后您将会看到组件消失。

### Re-renders when state updates {/*re-renders-when-state-updates*/}
### 状态更新时重新渲染 {/*re-renders-when-state-updates*/}

Once the component has been initially rendered, you can trigger further renders by updating its state with [`setState`](reference/setstate). Updating your component's state automatically queues a render. (You can imagine these as a restaurant guest ordering tea, dessert, and all sorts of things after putting in their first order, depending on the state of their thirst or hunger.)
一旦组件被渲染初始化后,您可以通过使用 [`setState`](reference/setstate) 更新其状态来触发之后的渲染。更新组件的状态会自动将渲染送入队列。(您可以想象这种情况为餐厅客人于第一次下单之后点了一份茶或点心或各种东西,具体取决于他们的状态现在是饿了或是渴了。)
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

<IllustrationBlock sequential>
<Illustration caption="State update..." alt="React as a server in a restaurant, serving a Card UI to the user, represented as a patron with a cursor for their head. They patron expresses they want a pink card, not a black one!" src="/images/docs/illustrations/i_rerender1.png" />
<Illustration caption="...triggers..." alt="React returns to the Component Kitchen and tells the Card Chef they need a pink Card." src="/images/docs/illustrations/i_rerender2.png" />
<Illustration caption="...render!" alt="The Card Chef gives React the pink Card." src="/images/docs/illustrations/i_rerender3.png" />
<Illustration caption="状态更新..." alt="React as a server in a restaurant, serving a Card UI to the user, represented as a patron with a cursor for their head. They patron expresses they want a pink card, not a black one!" src="/images/docs/illustrations/i_rerender1.png" />
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
<Illustration caption="...触发..." alt="React returns to the Component Kitchen and tells the Card Chef they need a pink Card." src="/images/docs/illustrations/i_rerender2.png" />
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
<Illustration caption="...渲染!" alt="The Card Chef gives React the pink Card." src="/images/docs/illustrations/i_rerender3.png" />
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
</IllustrationBlock>

## Step 2: React renders your components {/*step-2-react-renders-your-components*/}
## 步骤 2: React 渲染您的组件 {/*step-2-react-renders-your-components*/}

After you trigger a render, React calls your components to figure out what to display on screen. **"Rendering" is React calling your components.**
在你触发渲染后,React 会调用你的组件来确定要在屏幕上显示的内容。**"渲染中" 即 React 在调用您的组件。**

* **On initial render,** React will call the root component.
* **For subsequent renders,** React will call the function component whose state update triggered the render.
* **当第一次渲染,** React 会调用根组件
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
* **对于后续的渲染,** React 将调用其状态更新触发了渲染的函数组件。
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

This process is recursive: if the updated component returns some other component, React will render _that_ component next, and if that component also returns something, it will render _that_ component next, and so on. The process will continue until there are no more nested components and React knows exactly what should be displayed on screen.
这个处理是递归的:如果更新组件返回了一些其他的组件,React 接下来将会渲染 _那个_ 组件,同时如果那个组件也返回一些其他的组件,接下来也会渲染 _那个_ 组件,返回如此。这个过程会持续下去直到没有更多的嵌套组件而 React 也就会确切地知道哪些东西应该显示到屏幕上了。
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

In the following example, React will call `Gallery()` and `Image()` several times:
在接下来的例子中,React 将会调用 `Gallery()` `Image()` 若干次:

<Sandpack>

```js Gallery.js active
export default function Gallery() {
return (
<section>
<h1>Inspiring Sculptures</h1>
<h1>鼓舞人心的雕塑</h1>
<Image />
<Image />
<Image />
Expand Down Expand Up @@ -128,34 +130,34 @@ img { margin: 0 10px 10px 0; }

</Sandpack>

* **During the initial render,** React will [create the DOM nodes](https://developer.mozilla.org/docs/Web/API/Document/createElement) for `<section>`, `<h1>`, and three `<img>` tags.
* **During a re-render,** React will calculate which of their properties, if any, have changed since the previous render. It won't do anything with that information until the next step, the commit phase.
* **在初次渲染中,** React 将会为`<section>`,`<h1>` 和三个 `<img>` 标签 [创建 DOM 节点](https://developer.mozilla.org/docs/Web/API/Document/createElement)
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
* **在一次重渲染过程中,** React 将计算它们的哪些属性(如果有)自上次渲染以来已更改。在下一步(提交阶段)之前,它不会对这些信息执行任何操作。
Copy link
Contributor

@Neo42 Neo42 Jan 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* **在一次重渲染过程中,** React 将计算它们的哪些属性(如果有)自上次渲染以来已更改。在下一步(提交阶段)之前,它不会对这些信息执行任何操作。
* **在一次重渲染过程中**React 将计算它们的哪些属性(如果有的话)自上次渲染以来已更改。在下一步(提交阶段)之前,它不会对这些信息执行任何操作。

Copy link
Contributor Author

@oceanlvr oceanlvr Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我认为这里应该强调的是属性的更改,而不是是否有属性。
“如果它们有属性的话,那么 React 将计算它们的哪些属性自上次渲染以来已更改”,是否可能会给读者重心放在组件是否有属性的问题上(而不是更改上)。您觉得呢?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我明白你的意思。那把你的译法改成“(如果有的话)”可以吗?因为只写“如果有”的话,读起来会不太自然。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,我修改下。最初的版本确实不太顺口。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我把我的 suggestion 改了,你直接应用就可以,下面那条也是。

Copy link
Contributor

@Neo42 Neo42 Jan 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* **在一次重渲染过程中,** React 将计算它们的哪些属性(如果有)自上次渲染以来已更改。在下一步(提交阶段)之前,它不会对这些信息执行任何操作。
* **在一次重渲染过程中**React 将计算它们的哪些属性(如果有的话)自上次渲染以来已更改。在下一步(提交阶段)之前,它不会对这些信息执行任何操作。


<Gotcha>

Rendering must always be a [pure calculation](/learn/keeping-components-pure):
渲染必须总是一次 [纯计算](/learn/keeping-components-pure):
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

* **Same inputs, same output.** Given the same inputs, a component should always return the same JSX. (When someone orders a salad with tomatoes, they should not receive a salad with onions!)
* **Mind its own business.** It should not change any objects or variables that existed before rendering. (One order should not change anyone else's order.)
* **一些输入,一些输出。** 给定相同的输入,组件应始终返回相同的 JSX。(当有人点了西红柿沙拉时,他们不应该收到洋葱沙拉!)
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
* **管好它自己的事情。** 它不应更改渲染之前存在的任何对象或变量。(一个订单不应更改其他任何人的订单。)
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

Otherwise, you can encounter confusing bugs and unpredictable behavior as your codebase grows in complexity. When developing in "Strict Mode," React calls each component's function twice, which can help surface mistakes caused by impure functions.
否则,随着代码库复杂性的增加,您可能会遇到令人困惑的错误和不可预测的行为。在"严格模式"下开发时,React 会调用每个组件的函数两次,这可以帮助发现由不纯函数引起的错误。

</Gotcha>

<DeepDive title="Optimizing performance">
<DeepDive title="性能优化">

The default behavior of rendering all components nested within the updated component is not optimal for performance if the updated component is very high in the tree. If you run into a performance issue, there are several opt-in ways to solve it described in the [Performance](/learn/performance) section. **Don't optimize prematurely!**
如果更新的组件在树中的位置非常高,渲染所有嵌套的组件并且带有更新后的组件这种默认行为不是性能上的最佳选择。如果您遇到了性能问题,[性能](/learn/performance) 章节描述了几种可选的解决方案 。**不要过早优化!**
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

</DeepDive>

## Step 3: React commits changes to the DOM {/*step-3-react-commits-changes-to-the-dom*/}
## 步骤 3: React 提交更改到 DOM {/*step-3-react-commits-changes-to-the-dom*/}
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

After rendering (calling) your components, React will modify the DOM.
在(调用)渲染你的组件之后,React 将会修改 DOM
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

* **For the initial render,** React will use the [`appendChild()`](https://developer.mozilla.org/docs/Web/API/Node/appendChild) DOM API to put all the DOM nodes it has created on screen.
* **For re-renders,** React will apply the minimal necessary operations (calculated while rendering!) to make the DOM match the latest rendering output.
* **对于初次渲染,** React 会使用 [`appendChild()`](https://developer.mozilla.org/docs/Web/API/Node/appendChild) DOM API 以将其创建的所有 DOM 节点并放在屏幕上。
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
* **对于重渲染,** React 将应用最少的必要操作(在渲染时计算!),以使 DOM 与最新的渲染输出匹配。
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

**React only changes the DOM nodes if there's a difference between renders.** For example, here is a component that re-renders with different props passed from its parent every second. Notice how you can add some text into the `<input>`, updating its `value`, but the text doesn't disappear when the component re-renders:
**React 仅在渲染之间存在差异时更改 DOM 节点。** 例如,有一个组件,它每秒使用从父组件传递下来的不同属性重新渲染。注意,您如果添加一些文本到 `<input>` 标签,更新它的 `value`,但是文本不会在组件重渲染时消失:
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

<Sandpack>

Expand Down Expand Up @@ -195,21 +197,21 @@ export default function App() {

</Sandpack>

This works because during this last step, React only updates the content of `<h1>` with the new `time`. It sees that the `<input>` appears in the JSX in the same place as last time, so React doesn't touch the `<input>`—or its `value`!
## Epilogue: Browser paint {/*epilogue-browser-paint*/}
这是有效的是因为在最后一步中,React 只使用最新的 `time` 更新 `<h1>` 标签的内容。它看到 `<input>` 标签出现在 JSX 中与上次相同的位置,因此 React 不会触碰 `<input>` 标签或它的 `value`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
这是有效的是因为在最后一步中,React 只使用最新的 `time` 更新 `<h1>` 标签的内容。它看到 `<input>` 标签出现在 JSX 中与上次相同的位置,因此 React 不会触碰 `<input>` 标签或它的 `value`
这个例子之所以会正常运行,是因为在最后一步中,React 只会使用最新的 `time` 更新 `<h1>` 标签的内容。它看到 `<input>` 标签出现在 JSX 中与上次相同的位置,因此 React 不会动 `<input>` 标签或它的 `value`

oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
## 尾声:浏览器绘制 {/*epilogue-browser-paint*/}

After rendering is done and React updated the DOM, the browser will repaint the screen. Although this process is known as "browser rendering", we'll refer to it as "painting" to avoid confusion in the rest of these docs.
在渲染完成之后 React 会更新 DOM,浏览器会重绘这个屏幕,尽管这个过程被称之为 “浏览器重绘” ("browser rendering"),我们将它称为 “绘制” ("painting"),以避免在这些文档的其余部分中混淆。
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

<Illustration alt="A browser painting 'still life with card element'." src="/images/docs/illustrations/i_browser-paint.png" />
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

<Recap>

* Any screen update in a React app happens in three steps:
1. Trigger
2. Render
3. Commit
* You can use Strict Mode to find mistakes in your components
* React does not touch the DOM if the rendering result is the same as last time
* 在一个 React 应用中一次屏幕更新都会发生以下三个步骤:
1. 触发
2. 渲染
3. 提交
* 您可以使用严格模式去找到组件中的错误
* React 不会去修改重渲染结果与上次一样的 DOM
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved

</Recap>