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 reference/render.md #632

Merged
merged 18 commits into from
Nov 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions beta/src/pages/reference/render.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
---
title: render()
translators:
- liu-jin-yi
QC-L marked this conversation as resolved.
Show resolved Hide resolved
- QC-L
---

<Intro>

`render` renders a piece of [JSX](/learn/writing-markup-with-jsx) ("React element") into a browser DOM container node. It instructs React to change the DOM inside of the `container` so that it matches the passed JSX.
`render` 函数会将 [JSX](/learn/writing-markup-with-jsx)(“React 元素”)渲染到浏览器 DOM 容器节点中。它可以让 React 改变 `container` 中 DOM,使其与传递的 JSX 相匹配。
Copy link
Contributor

@Neo42 Neo42 Dec 14, 2021

Choose a reason for hiding this comment

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

修改建议:

  1. ”它可以让 React 改变 container 中的 DOM“
  2. ”所传递的 JSX“


```js
render(<App />, container);
Expand All @@ -13,21 +16,21 @@ render(<App />, container, callback);

</Intro>

## Rendering the root component {/*rendering-the-root-component*/}
## 渲染根组件 {/*rendering-the-root-component*/}

To call `render`, you need a piece of JSX and a DOM container:
如需调用 `render`,需要编写一段 JSX 代码以及一个 DOM 容器:

<APIAnatomy>

<AnatomyStep title="React element">

The UI you want to render.
需要渲染的 UI 界面。

</AnatomyStep>

<AnatomyStep title="DOM container">

The DOM node you want to render your UI into. The container itself isn’t modified, only its children are.
用于渲染 UI 界面的 DOM 节点。该容器不能被修改,只能修改它的子节点。
Copy link
Contributor

Choose a reason for hiding this comment

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

这句明显错了,应该是"该容器本身不会被修改"、”只有其子节点会被修改“


</AnatomyStep>

Expand All @@ -38,7 +41,7 @@ render(<App />, container);

</APIAnatomy>

In apps fully built with React, you will do this once at the top level of your app--to render the "root" component.
在完全由 React 构建的应用程序中,你需要在你的应用程序的入口文件执行该操作(渲染 "root" 组件)。
Copy link
Contributor

Choose a reason for hiding this comment

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

”在完全 使用 React 构建的应用程序中,你需要在你应用程序的最顶层执行一次该操作——渲染 "根" 组件。“

  1. with 应该翻译成使用
  2. 入口文件 entry file 是 webpack 中的概念,不应该用在这里,因为不是所有 react 应用都是用 webpack 写的,建议按原文直译


<Sandpack>

Expand All @@ -52,24 +55,24 @@ render(<App />, document.getElementById('root'));

```js App.js
export default function App() {
return <h1>Hello, world!</h1>;
return <h1>你好,世界!</h1>;
}
```

</Sandpack>

<br />

## Rendering multiple roots {/*rendering-multiple-roots*/}
## 渲染多个根组件 {/*rendering-multiple-roots*/}

If you use ["sprinkles"](/learn/add-react-to-a-website) of React here and there, call `render` for each top-level piece of UI managed by React.
不管你用 ["哪种方式"](/learn/add-react-to-a-website) 使用 React,你都需为每个由 React 管理的顶层 UI 组件调用 `render` 函数进行渲染。

<Sandpack>

```html public/index.html
<nav id="navigation"></nav>
<main>
<p>This paragraph is not rendered by React (open index.html to verify).</p>
<p>这一段没有被 React 渲染(可以查看 index.html 进行验证)。</p>
<section id="comments"></section>
</main>
```
Expand Down Expand Up @@ -112,8 +115,8 @@ export function Comments() {
return (
<>
<h2>Comments</h2>
<Comment text="Hello!" author="Sophie" />
<Comment text="How are you?" author="Sunil" />
<Comment text="你好!" author="Sophie" />
<Comment text="你是谁?" author="Sunil" />
Copy link
Contributor

Choose a reason for hiding this comment

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

你好吗?

How are you 不是 who are you

</>
);
}
Expand All @@ -134,9 +137,9 @@ nav ul li { display: inline-block; margin-right: 20px; }

<br />

## Updating the rendered tree {/*updating-the-rendered-tree*/}
## 更新已渲染的 DOM {/*updating-the-rendered-tree*/}
Copy link
Contributor

Choose a reason for hiding this comment

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

建议改成 更新已渲染的树 或 更新已渲染的组件树,因为组件树是 React 中的,DOM 树是浏览器中的,两者不能混淆,而且下文中有对两者关系的叙述,应该区别清楚,避免增加理解难度


You can call `render` more than once on the same DOM node. As long as the component tree structure matches up with what was previously rendered, React will [preserve the state](/learn/preserving-and-resetting-state). Notice how you can type in the input:
你可以在同一 DOM 节点上多次调用 `render`。只要组件树结构与之前渲染的内容一致,React 就会 [保留该状态](/learn/preserving-and-resetting-state) 。请仔细观察在输入框中输入内容后的效果:

<Sandpack>

Expand All @@ -159,30 +162,30 @@ export default function App({counter}) {
return (
<>
<h1>Hello, world! {counter}</h1>
<input placeholder="Type something here" />
<input placeholder="在这里输入一些东西" />
</>
);
}
```

</Sandpack>

You can destroy the rendered tree with [`unmountComponentAtNode()`](TODO).
你可以使用 [`unmountComponentAtNode()`](TODO) 来销毁已渲染的 DOM 树。

<br />

## When not to use it {/*when-not-to-use-it*/}
## 何时不使用它 {/*when-not-to-use-it*/}

* If your app uses server rendering and generates HTML on the server, use [`hydrate`](TODO) instead of `render`.
* If your app is fully built with React, you shouldn't need to use `render` more than once. If you want to render something in a different part of the DOM tree (for example, a modal or a tooltip), use [`createPortal`](TODO) instead.
* 如果你的应用程序使用服务器渲染,并会在服务器上生成 HTML,请使用 [`hydrate`](TODO) 函数,而非 `render` 函数。
* 如果你的应用程序完全基于 React 构建,你大概率不需要多次使用 `render` 函数。如果你想在 DOM 树的其他位置渲染内容(例如,modal 或者 tooltip),那么请使用 [`createPortal`](TODO) 来代替。

<br />


## Behavior in detail {/*behavior-in-detail*/}
## 行为细节 {/*behavior-in-detail*/}
Copy link
Contributor

Choose a reason for hiding this comment

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

behavior 除了可以指行为,还可以指特性,这里译为特性更合适


The first time you call `render`, any existing DOM elements inside `container` are replaced. If you call `render` again, React will update the DOM as necessary to reflect the latest JSX. React will decide which parts of the DOM can be reused and which need to be recreated by ["matching it up"](/learn/preserving-and-resetting-state) with the previously rendered tree. Calling `render` repeatedly is similar to calling `setState`--in both cases, React avoids unnecessary DOM updates.
在你第一次调用 `render` 时,`container` 内的任何已有 DOM 元素都会被替换。如果你再次调用 `render` 时,React 将会通过与先前渲染的组件树 ["匹配"](/learn/preserving-and-resetting-state) 的方式,来决定 DOM 的哪些部分可以重用,哪些需要重新创建。重复调用 `render` 与调用 `setState` 效果类似。无论哪种情况,React 都会避免不必要的 DOM 更新。
Copy link
Contributor

Choose a reason for hiding this comment

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

如果你再次调用 render 的话,React 会为了体现最新的 JSX 而进行必要的 DOM 更新。React 会通过将 DOM 与先前渲染的组件树进行 "匹配" 的方式,来决定 DOM 的哪些部分可以重用,哪些需要重新创建。重复调用 render 与调用 setState 类似——在这两种情况下,React 都会避免不必要的 DOM 更新。

  1. 这里”React will update the DOM as necessary to reflect the latest JSX.“漏译了
  2. matching it up 中的 it 指的是 DOM,应该翻译出来
  3. 建议把”效果类似“中的”效果“去掉。不应该说两种调用的效果类似,因为本质上 render 是负责挂载组件的而 setState 是负责改变 state 的。只能说在避免不必要的 DOM 更新方面两者是类似的。


You can pass a callback as the third argument. React will call it after your component is in the DOM.
你可以将一个回调函数,作为 `render` 的第三个参数。React 会在你的组件在 DOM 中出现后,调用它。
Copy link
Contributor

Choose a reason for hiding this comment

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

你可以将一个回调函数作为第三个参数传递给 render。React 会在你的组件在 DOM 中出现后调用它。

  1. pass 漏译了
  2. 谓语之前不要加逗号


If you render `<MyComponent />`, and `MyComponent` is a class component, `render` will return the instance of that class. In all other cases, it will return `null`.
如果需要渲染 `<MyComponent />` ,并且 `MyComponent` 是一个类组件,`render` 函数将返回该类的实例。在其他情况下,它将返回 `null`