Skip to content

Commit 538ffdc

Browse files
ppplutoQC-L
authored andcommitted
docs(cn): translate content/docs/conditional-rendering.md into Chinese (#92)
<!-- Thank you for the PR! Contributors like you keep React awesome! Please see the Contribution Guide for guidelines: https://github.com/reactjs/reactjs.org/blob/master/CONTRIBUTING.md If your PR references an existing issue, please add the issue number below -->
1 parent bebb416 commit 538ffdc

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

content/docs/conditional-rendering.md

+28-28
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ redirect_from:
88
- "tips/false-in-jsx.html"
99
---
1010

11-
In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending on the state of your application.
11+
React 中,你可以创建不同的组件来封装各种你需要的行为。然后,依据应用的不同状态,你可以只渲染对应状态下的部分内容。
1212

13-
Conditional rendering in React works the same way conditions work in JavaScript. Use JavaScript operators like [`if`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) or the [conditional operator](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) to create elements representing the current state, and let React update the UI to match them.
13+
React 中的条件渲染和 JavaScript 中的一样,使用 JavaScript 运算符 [`if`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) 或者[条件运算符](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)去创建元素来表现当前的状态,然后让 React 根据它们来更新 UI。
1414

15-
Consider these two components:
15+
观察这两个组件:
1616

1717
```js
1818
function UserGreeting(props) {
@@ -24,7 +24,7 @@ function GuestGreeting(props) {
2424
}
2525
```
2626

27-
We'll create a `Greeting` component that displays either of these components depending on whether a user is logged in:
27+
再创建一个 `Greeting` 组件,它会根据用户是否登录来决定显示上面的哪一个组件。
2828

2929
```javascript{3-7,11,12}
3030
function Greeting(props) {
@@ -42,15 +42,15 @@ ReactDOM.render(
4242
);
4343
```
4444

45-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/ZpVxNq?editors=0011)
45+
[**CodePen 上尝试**](https://codepen.io/gaearon/pen/ZpVxNq?editors=0011)
4646

47-
This example renders a different greeting depending on the value of `isLoggedIn` prop.
47+
这个示例根据 `isLoggedIn` 的值来渲染不同的问候语。
4848

49-
### Element Variables
49+
### 元素变量
5050

51-
You can use variables to store elements. This can help you conditionally render a part of the component while the rest of the output doesn't change.
51+
你可以使用变量来储存元素。 它可以帮助你有条件地渲染组件的一部分,而其他的渲染部分并不会因此而改变。
5252

53-
Consider these two new components representing Logout and Login buttons:
53+
观察这两个组件,它们分别代表了注销和登录按钮:
5454

5555
```js
5656
function LoginButton(props) {
@@ -70,9 +70,9 @@ function LogoutButton(props) {
7070
}
7171
```
7272

73-
In the example below, we will create a [stateful component](/docs/state-and-lifecycle.html#adding-local-state-to-a-class) called `LoginControl`.
73+
在下面的示例中,我们将创建一个名叫 `LoginControl`[有状态的组件](/docs/state-and-lifecycle.html#adding-local-state-to-a-class)
7474

75-
It will render either `<LoginButton />` or `<LogoutButton />` depending on its current state. It will also render a `<Greeting />` from the previous example:
75+
它将根据当前的状态来渲染 `<LoginButton />` 或者 `<LogoutButton />`。同时它还会渲染上一个示例中的 `<Greeting />`
7676

7777
```javascript{20-25,29,30}
7878
class LoginControl extends React.Component {
@@ -116,13 +116,13 @@ ReactDOM.render(
116116
);
117117
```
118118

119-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/QKzAgB?editors=0010)
119+
[**CodePen 上尝试**](https://codepen.io/gaearon/pen/QKzAgB?editors=0010)
120120

121-
While declaring a variable and using an `if` statement is a fine way to conditionally render a component, sometimes you might want to use a shorter syntax. There are a few ways to inline conditions in JSX, explained below.
121+
声明一个变量并使用 `if` 语句进行条件渲染是不错的方式,但有时你可能会想使用更为简洁的语法。接下来,我们将介绍几种在 JSX 中内联条件渲染的方法。
122122

123-
### Inline If with Logical && Operator
123+
### 与运算符 &&
124124

125-
You may [embed any expressions in JSX](/docs/introducing-jsx.html#embedding-expressions-in-jsx) by wrapping them in curly braces. This includes the JavaScript logical `&&` operator. It can be handy for conditionally including an element:
125+
通过花括号包裹代码,你可以[JSX 中嵌入任何表达式](/docs/introducing-jsx.html#embedding-expressions-in-jsx)。这也包括 JavaScript 中的逻辑与 (&&) 运算符。它可以很方便地进行元素的条件渲染。
126126

127127
```js{6-10}
128128
function Mailbox(props) {
@@ -146,17 +146,17 @@ ReactDOM.render(
146146
);
147147
```
148148

149-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/ozJddz?editors=0010)
149+
[**CodePen 上尝试**](https://codepen.io/gaearon/pen/ozJddz?editors=0010)
150150

151-
It works because in JavaScript, `true && expression` always evaluates to `expression`, and `false && expression` always evaluates to `false`.
151+
之所以能这样做,是因为在 JavaScript 中,`true && expression` 总是会返回 `expression`, `false && expression` 总是会返回 `false`
152152

153-
Therefore, if the condition is `true`, the element right after `&&` will appear in the output. If it is `false`, React will ignore and skip it.
153+
因此,如果条件是 `true``&&` 右侧的元素就会被渲染,如果是 `false`React 会忽略并跳过它。
154154

155-
### Inline If-Else with Conditional Operator
155+
### 三目运算符
156156

157-
Another method for conditionally rendering elements inline is to use the JavaScript conditional operator [`condition ? true : false`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator).
157+
另一种内联条件渲染的方法是使用 JavaScript 中的三目运算符 [`condition ? true : false`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)
158158

159-
In the example below, we use it to conditionally render a small block of text.
159+
在下面这个示例中,我们用它来条件渲染一小段文本
160160

161161
```javascript{5}
162162
render() {
@@ -169,7 +169,7 @@ render() {
169169
}
170170
```
171171

172-
It can also be used for larger expressions although it is less obvious what's going on:
172+
同样的,它也可以用于较为复杂的表达式中,虽然看起来不是很直观:
173173

174174
```js{5,7,9}
175175
render() {
@@ -186,13 +186,13 @@ render() {
186186
}
187187
```
188188

189-
Just like in JavaScript, it is up to you to choose an appropriate style based on what you and your team consider more readable. Also remember that whenever conditions become too complex, it might be a good time to [extract a component](/docs/components-and-props.html#extracting-components).
189+
就像在 JavaScript 中一样,你可以根据团队的习惯来选择可读性更高的代码风格。需要注意的是,如果条件变得过于复杂,那你应该考虑如何[提取组件](/docs/components-and-props.html#extracting-components)
190190

191-
### Preventing Component from Rendering
191+
### 阻止组件渲染
192192

193-
In rare cases you might want a component to hide itself even though it was rendered by another component. To do this return `null` instead of its render output.
193+
在极少数情况下,你可能希望能隐藏组件,即使它已经被其他组件渲染。若要完成此操作,你可以让 `render` 方法直接返回 `null`,而不进行任何渲染。
194194

195-
In the example below, the `<WarningBanner />` is rendered depending on the value of the prop called `warn`. If the value of the prop is `false`, then the component does not render:
195+
下面的示例中,`<WarningBanner />` 会根据 prop `warn` 的值来进行条件渲染。如果 `warn` 的值是 `false`,那么组件则不会渲染:
196196

197197
```javascript{2-4,29}
198198
function WarningBanner(props) {
@@ -238,6 +238,6 @@ ReactDOM.render(
238238
);
239239
```
240240

241-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/Xjoqwm?editors=0010)
241+
[**CodePen 上尝试**](https://codepen.io/gaearon/pen/Xjoqwm?editors=0010)
242242

243-
Returning `null` from a component's `render` method does not affect the firing of the component's lifecycle methods. For instance `componentDidUpdate` will still be called.
243+
在组件的 `render` 方法中返回 `null` 并不会影响组件的生命周期。例如,上面这个示例中,`componentDidUpdate` 依然会被调用。

0 commit comments

Comments
 (0)