You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/conditional-rendering.md
+32-30
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
1
---
2
2
id: conditional-rendering
3
-
title: Conditional Rendering
3
+
title: Render Có Điều Kiện
4
4
permalink: docs/conditional-rendering.html
5
5
prev: handling-events.html
6
6
next: lists-and-keys.html
7
7
redirect_from:
8
8
- "tips/false-in-jsx.html"
9
9
---
10
10
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
+
Trong React, bạn có thể tạo ra các component riêng biệt chứa đựng hành vi mà bạn cần. Tiếp đến, dựa trên trạng thái (state) hiện tại của ứng dụng (application), bạn sẽ chỉ định việc các component đó có nên xuất hiện hay không.
12
12
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
+
Render có điều kiện trong React hoạt động tương tự như cách mà chúng ta vẫn thường thấy trong Javascript. Đó là dùng câu lệnh [`if`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)hay [conditional operator](https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)trong Javascript để tạo ra các element ứng với state hiện tại, React sau đó sẽ cập nhật giao diện người dùng (UI) phù hợp với state đó.
14
14
15
-
Consider these two components:
15
+
Thử xét đến hai component bên dưới:
16
16
17
17
```js
18
18
functionUserGreeting(props) {
@@ -24,7 +24,7 @@ function GuestGreeting(props) {
24
24
}
25
25
```
26
26
27
-
We'll create a `Greeting`component that displays either of these components depending on whether a user is logged in:
27
+
Ta tạo một component với tên `Greeting`, nhiệm vụ của nó là hiển thị một trong hai component phía trên dựa vào trạng thái của người dùng (đã đăng nhập hay chưa).
28
28
29
29
```javascript{3-7,11,12}
30
30
function Greeting(props) {
@@ -36,21 +36,22 @@ function Greeting(props) {
36
36
}
37
37
38
38
ReactDOM.render(
39
-
// Try changing to isLoggedIn={true}:
39
+
// Thử thay đổi prop isLoggedIn={true}:
40
40
<Greeting isLoggedIn={false} />,
41
41
document.getElementById('root')
42
42
);
43
43
```
44
44
45
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/ZpVxNq?editors=0011)
45
+
[**Thử trên CodePen**](https://codepen.io/gaearon/pen/ZpVxNq?editors=0011)
46
46
47
-
This example renders a different greeting depending on the value of `isLoggedIn` prop.
47
+
Ví dụ này sẽ hiển thị nội dung lời chào khác nhau dựa trên giá trị của prop `isLoggedIn`.
48
48
49
-
### Element Variables {#element-variables}
49
+
### Gán giá trị element vào biến {#element-variables}
50
50
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
+
Bạn có thể dùng biến để lưu lại các element. Điều này giúp cho bạn có thể render có điều kiện một phần của component trong khi phần còn lại của component sẽ không bị thay đổi.
52
+
53
+
Thử khởi tạo hai component thể hiện nút đăng nhập (Login) và đăng xuất (Logout):
52
54
53
-
Consider these two new components representing Logout and Login buttons:
54
55
55
56
```js
56
57
functionLoginButton(props) {
@@ -70,9 +71,9 @@ function LogoutButton(props) {
70
71
}
71
72
```
72
73
73
-
In the example below, we will create a [stateful component](/docs/state-and-lifecycle.html#adding-local-state-to-a-class)called`LoginControl`.
74
+
Trong ví dụ phía dưới, chúng ta sẽ tạo một [stateful component](/docs/state-and-lifecycle.html#adding-local-state-to-a-class)với tên gọi`LoginControl`.
74
75
75
-
It will render either `<LoginButton />`or`<LogoutButton />`depending on its current state. It will also render a `<Greeting />`from the previous example:
76
+
Component ta vừa tạo sẽ hiển thị hoặc `<LoginButton />`hoặc`<LogoutButton />`dựa theo state hiện tại. Nó cũng sẽ hiển thị component `<Greeting />`từ ví dụ trước đó.
76
77
77
78
```javascript{20-25,29,30}
78
79
class LoginControl extends React.Component {
@@ -116,13 +117,13 @@ ReactDOM.render(
116
117
);
117
118
```
118
119
119
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/QKzAgB?editors=0010)
120
+
[**Thử trên CodePen**](https://codepen.io/gaearon/pen/QKzAgB?editors=0010)
120
121
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.
122
+
Trong khi việc khởi tạo một biến và sử dụng lệnh `if`là cách làm khá ổn để render có điều kiện một component, nhưng đôi khi, bạn sẽ muốn dùng những cú pháp ngắn hơn. Thật may là chúng ta vẫn có một số ít lựa chọn khác để có thể thực hiện render có điều kiện trực tiếp trên JSX. Chúng sẽ được giải thích rõ bên dưới.
122
123
123
-
### Inline If with Logical && Operator {#inline-if-with-logical--operator}
124
+
### Thay thế If bằng toán tử logic && {#inline-if-with-logical--operator}
124
125
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:
126
+
Bạn có thể nhúng toán tử logic `&&` cũng như [nhúng các biểu thức vào JSX](/docs/introducing-jsx.html#embedding-expressions-in-jsx)bằng cách bọc chúng lại trong cặp ngoặc nhọn `{}`. Việc này rất hữu ích khi xử lí các điều kiện có element bên trong.
126
127
127
128
```js{6-10}
128
129
function Mailbox(props) {
@@ -146,17 +147,18 @@ ReactDOM.render(
146
147
);
147
148
```
148
149
149
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/ozJddz?editors=0010)
150
+
[**Thử trên CodePen**](https://codepen.io/gaearon/pen/ozJddz?editors=0010)
151
+
152
+
Đoạn code vừa rồi sẽ hoạt động vì trong Javascript, giá trị của `true && expression` luôn dựa vào `expression`, còn `false && expression` thì sẽ luôn được hiểu là `false`.
150
153
151
-
It works because in JavaScript, `true && expression` always evaluates to `expression`, and `false && expression` always evaluates to `false`.
154
+
Vì thế, nếu điều kiện trả về giá trị là `true`, element phía sau toán tử logic `&&` sẽ xuất hiện ở màn hình. Nếu giá trị trả về là `false`, React sẽ bỏ qua nó.
152
155
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.
156
+
### Thay thế If-Else bằng toán tử điều kiện {#inline-if-else-with-conditional-operator}
154
157
155
-
### Inline If-Else with Conditional Operator {#inline-if-else-with-conditional-operator}
158
+
Một phương thức khác dùng để thực hiện render có điều kiện trực tiếp trên JSX là dùng toán tử điều kiện (ba ngôi) [`condition ? true : false`](https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Operators/Conditional_Operator).
156
159
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).
158
160
159
-
In the example below, we use it to conditionally render a small block of text.
161
+
Trong ví dụ phía dưới, ta sử dụng phương thức đã được nêu ở trên để render có điều kiện một đoạn văn bản nhỏ.
160
162
161
163
```javascript{5}
162
164
render() {
@@ -169,7 +171,7 @@ render() {
169
171
}
170
172
```
171
173
172
-
It can also be used for larger expressions although it is less obvious what's going on:
174
+
Nó cũng có thể được sử dụng ở các biểu thức (expressions) lớn hơn, mặc dù điều đó có thể làm cho chúng ta khó hiểu rõ việc gì đang xảy ra.
173
175
174
176
```js{5,7,9}
175
177
render() {
@@ -186,13 +188,13 @@ render() {
186
188
}
187
189
```
188
190
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).
191
+
Giống hệt như Javascript, việc sử dụng cách thức nào sẽ dựa trên những gì mà bạn và nhóm của bạn thấy dễ đọc hơn. Và nên lưu ý, khi các điều kiện trở nên quá phức tạp thì nên cân nhắc đến việc thực hiện [tách component](/docs/components-and-props.html#extracting-components).
190
192
191
-
### Preventing Component from Rendering {#preventing-component-from-rendering}
193
+
### Ngăn chặn component thực hiện render {#preventing-component-from-rendering}
192
194
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.
195
+
Trong một số trường hợp hiếm gặp, bạn sẽ muốn một component tự ẩn đi dù nó được render bởi một component khác. Để làm được điều đó, ta sẽ trả về `null`thay vì trả về những gì cần hiện lên màn hình.
194
196
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:
197
+
Ở ví dụ phía dưới, component`<WarningBanner />`được render dựa trên giá trị của prop `warn`. Nếu giá trị của prop là`false` thì component đó sẽ không được render.
196
198
197
199
```javascript{2-4,29}
198
200
function WarningBanner(props) {
@@ -238,6 +240,6 @@ ReactDOM.render(
238
240
);
239
241
```
240
242
241
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/Xjoqwm?editors=0010)
243
+
[**Thử trên CodePen**](https://codepen.io/gaearon/pen/Xjoqwm?editors=0010)
242
244
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.
245
+
Trả về `null`bên trong hàm`render`của component không gây ảnh hưởng đến các phương thức của lifecycle. Ví dụ như `componentDidUpdate`vẫn sẽ được gọi.
0 commit comments