Skip to content

Commit 36bea19

Browse files
liu-jin-yiQC-Lliujinyi
authored
[Beta] docs(cn): translate reference/render.md (#632)
Co-authored-by: QiChang Li <github@liqichang.com> Co-authored-by: liujinyi <liujinyi@joinfun.com.cn>
1 parent 3ab0d62 commit 36bea19

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

beta/src/pages/reference/render.md

+26-23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
---
22
title: render()
3+
translators:
4+
- liu-jin-yi
5+
- QC-L
36
---
47

58
<Intro>
69

7-
`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.
10+
`render` 函数会将 [JSX](/learn/writing-markup-with-jsx)(“React 元素”)渲染到浏览器 DOM 容器节点中。它可以让 React 改变 `container` 中 DOM,使其与传递的 JSX 相匹配。
811

912
```js
1013
render(<App />, container);
@@ -13,21 +16,21 @@ render(<App />, container, callback);
1316

1417
</Intro>
1518

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

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

2023
<APIAnatomy>
2124

2225
<AnatomyStep title="React element">
2326

24-
The UI you want to render.
27+
需要渲染的 UI 界面。
2528

2629
</AnatomyStep>
2730

2831
<AnatomyStep title="DOM container">
2932

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

3235
</AnatomyStep>
3336

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

3942
</APIAnatomy>
4043

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

4346
<Sandpack>
4447

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

5356
```js App.js
5457
export default function App() {
55-
return <h1>Hello, world!</h1>;
58+
return <h1>你好,世界!</h1>;
5659
}
5760
```
5861

5962
</Sandpack>
6063

6164
<br />
6265

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

65-
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.
68+
不管你用 ["哪种方式"](/learn/add-react-to-a-website) 使用 React,你都需为每个由 React 管理的顶层 UI 组件调用 `render` 函数进行渲染。
6669

6770
<Sandpack>
6871

6972
```html public/index.html
7073
<nav id="navigation"></nav>
7174
<main>
72-
<p>This paragraph is not rendered by React (open index.html to verify).</p>
75+
<p>这一段没有被 React 渲染(可以查看 index.html 进行验证)。</p>
7376
<section id="comments"></section>
7477
</main>
7578
```
@@ -112,8 +115,8 @@ export function Comments() {
112115
return (
113116
<>
114117
<h2>Comments</h2>
115-
<Comment text="Hello!" author="Sophie" />
116-
<Comment text="How are you?" author="Sunil" />
118+
<Comment text="你好!" author="Sophie" />
119+
<Comment text="你是谁?" author="Sunil" />
117120
</>
118121
);
119122
}
@@ -134,9 +137,9 @@ nav ul li { display: inline-block; margin-right: 20px; }
134137

135138
<br />
136139

137-
## Updating the rendered tree {/*updating-the-rendered-tree*/}
140+
## 更新已渲染的 DOM {/*updating-the-rendered-tree*/}
138141

139-
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:
142+
你可以在同一 DOM 节点上多次调用 `render`。只要组件树结构与之前渲染的内容一致,React 就会 [保留该状态](/learn/preserving-and-resetting-state) 。请仔细观察在输入框中输入内容后的效果:
140143

141144
<Sandpack>
142145

@@ -159,30 +162,30 @@ export default function App({counter}) {
159162
return (
160163
<>
161164
<h1>Hello, world! {counter}</h1>
162-
<input placeholder="Type something here" />
165+
<input placeholder="在这里输入一些东西" />
163166
</>
164167
);
165168
}
166169
```
167170

168171
</Sandpack>
169172

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

172175
<br />
173176

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

176-
* If your app uses server rendering and generates HTML on the server, use [`hydrate`](TODO) instead of `render`.
177-
* 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.
179+
* 如果你的应用程序使用服务器渲染,并会在服务器上生成 HTML,请使用 [`hydrate`](TODO) 函数,而非 `render` 函数。
180+
* 如果你的应用程序完全基于 React 构建,你大概率不需要多次使用 `render` 函数。如果你想在 DOM 树的其他位置渲染内容(例如,modal 或者 tooltip),那么请使用 [`createPortal`](TODO) 来代替。
178181

179182
<br />
180183

181184

182-
## Behavior in detail {/*behavior-in-detail*/}
185+
## 行为细节 {/*behavior-in-detail*/}
183186

184-
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.
187+
在你第一次调用 `render` 时,`container` 内的任何已有 DOM 元素都会被替换。如果你再次调用 `render` 时,React 将会通过与先前渲染的组件树 ["匹配"](/learn/preserving-and-resetting-state) 的方式,来决定 DOM 的哪些部分可以重用,哪些需要重新创建。重复调用 `render` 与调用 `setState` 效果类似。无论哪种情况,React 都会避免不必要的 DOM 更新。
185188

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

188-
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`.
191+
如果需要渲染 `<MyComponent />` ,并且 `MyComponent` 是一个类组件,`render` 函数将返回该类的实例。在其他情况下,它将返回 `null`

0 commit comments

Comments
 (0)