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: src/content/learn/state-as-a-snapshot.md
-10Lines changed: 0 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -74,20 +74,10 @@ Chúng ta sẽ xem xét kỹ hơn mối quan hệ giữa state và re-render tro
74
74
75
75
Không giống như một bức ảnh hay một khung hình phim, snapshot UI bạn trả về có tính tương tác. Nó bao gồm logic như event handler chỉ định điều gì xảy ra khi input thay đổi. React cập nhật màn hình để phù hợp với snapshot này và kết nối các event handler. Kết quả là, khi nhấn nút sẽ kích hoạt event handler từ JSX của bạn.
76
76
77
-
<<<<<<< HEAD
78
77
Khi React re-render một component:
79
78
1. React gọi lại hàm của bạn.
80
79
2. Hàm của bạn trả về một snapshot JSX mới.
81
80
3. React cập nhật màn hình sao cho tương đồng với snapshot bạn đã trả về.
82
-
=======
83
-
Unlike a photograph or a movie frame, the UI "snapshot" you return is interactive. It includes logic like event handlers that specify what happens in response to inputs. React updates the screen to match this snapshot and connects the event handlers. As a result, pressing a button will trigger the click handler from your JSX.
84
-
85
-
When React re-renders a component:
86
-
87
-
1. React calls your function again.
88
-
2. Your function returns a new JSX snapshot.
89
-
3. React then updates the screen to match the snapshot your function returned.
90
-
>>>>>>> 7bdbab144e09d4edf793ff5128080eb1dba79be4
91
81
92
82
<IllustrationBlocksequential>
93
83
<Illustration caption="React thực thi hàm của bạn" src="/images/docs/illustrations/i_render1.png" />
Copy file name to clipboardExpand all lines: src/content/learn/writing-markup-with-jsx.md
+44-46Lines changed: 44 additions & 46 deletions
Original file line number
Diff line number
Diff line change
@@ -1,42 +1,42 @@
1
1
---
2
-
title: Writing Markup with JSX
2
+
title: Viết Giao Diện Với JSX
3
3
---
4
4
5
5
<Intro>
6
6
7
-
*JSX*is a syntax extension for JavaScript that lets you write HTML-like markup inside a JavaScript file. Although there are other ways to write components, most React developers prefer the conciseness of JSX, and most codebases use it.
7
+
*JSX*là cú pháp mở rộng cho JavaScript cho phép bạn viết giao diện giống HTML bên trong JavaScript file. Mặc dù có nhiều cách khác để viết components, nhưng hầu hết các nhà phát triển React đều thích sự đơn giản của JSX và hầu hết các codebases đều sử dụng JSX.
8
8
9
9
</Intro>
10
10
11
11
<YouWillLearn>
12
12
13
-
*Why React mixes markup with rendering logic
14
-
*How JSX is different from HTML
15
-
*How to display information with JSX
13
+
*Tại sao React kết hợp giao diện với rendering logic
14
+
* JSX khác HTML như thế nào
15
+
*Hiển thị thông tin với JSX như thế nào
16
16
17
17
</YouWillLearn>
18
18
19
-
## JSX: Putting markup into JavaScript {/*jsx-putting-markup-into-javascript*/}
19
+
## JSX: Đưa giao diện vào JavaScript {/*jsx-putting-markup-into-javascript*/}
20
20
21
-
The Web has been built on HTML, CSS, and JavaScript. For many years, web developers kept content in HTML, design in CSS, and logic in JavaScript—often in separate files! Content was marked up inside HTML while the page's logic lived separately in JavaScript:
21
+
Web đã được xây dựng dựa trên HTML, CSS và JavaScript. Trong nhiều năm, các nhà phát triển web tạo dung bằng HTML, thiết kế bằng CSS và viết logic bằng JavaScript, thường ở các tệp riêng biệt! Nội dung được soạn bên trong HTML trong khi logic của trang tồn tại riêng biệt trong JavaScript:
22
22
23
23
<DiagramGroup>
24
24
25
-
<Diagramname="writing_jsx_html"height={237}width={325}alt="HTML markup with purple background and a div with two child tags: p and form.">
25
+
<Diagramname="writing_jsx_html"height={237}width={325}alt="Giao diện HTML có nền màu tím và một div có hai thẻ con: p và form.">
26
26
27
27
HTML
28
28
29
29
</Diagram>
30
30
31
-
<Diagramname="writing_jsx_js"height={237}width={325}alt="Three JavaScript handlers with yellow background: onSubmit, onLogin, and onClick.">
31
+
<Diagramname="writing_jsx_js"height={237}width={325}alt="Ba trình xử lý JavaScript có nền màu vàng: onSubmit, onLogin và onClick.">
32
32
33
33
JavaScript
34
34
35
35
</Diagram>
36
36
37
37
</DiagramGroup>
38
38
39
-
But as the Web became more interactive, logic increasingly determined content. JavaScript was in charge of the HTML! This is why **in React, rendering logic and markup live together in the same place—components.**
39
+
Nhưng khi Web có thể tương tác nhiều hơn, logic ngày càng quyết định nội dung của trang web. JavaScript chịu trách nhiệm render HTML! Đây là lý do tại sao **trong React, logic hiển thị và giao diện tồn tại cùng nhau ở cùng một nơi—components.**
40
40
41
41
<DiagramGroup>
42
42
@@ -54,19 +54,19 @@ But as the Web became more interactive, logic increasingly determined content. J
54
54
55
55
</DiagramGroup>
56
56
57
-
Keeping a button's rendering logic and markup together ensures that they stay in sync with each other on every edit. Conversely, details that are unrelated, such as the button's markup and a sidebar's markup, are isolated from each other, making it safer to change either of them on their own.
57
+
Việc giữ logic hiển thị và giao diện của một button cùng nhau đảm bảo rằng chúng luôn đồng bộ trong mỗi lần chỉnh sửa. Ngược lại, các thành phần không liên quan, chẳng hạn như giao diện của button và giao diện của sidebar, được tách biệt với nhau, giúp việc tự thay đổi một trong hai trở nên an toàn hơn.
58
58
59
-
Each React component is a JavaScript function that may contain some markup that React renders into the browser. React components use a syntax extension called JSX to represent that markup. JSX looks a lot like HTML, but it is a bit stricter and can display dynamic information. The best way to understand this is to convert some HTML markup to JSX markup.
59
+
Mỗi React component là một hàm JavaScript có thể chứa một số giao diện mà React hiển thị trên trình duyệt. Các React component sử dụng cú pháp mở rộng JSX để biểu đạt giao diện đó. JSX trông rất giống HTML, nhưng nó chặt chẽ hơn một chút và có thể hiển thị thông tin động. Cách tốt nhất để hiểu hơn về JSX là chuyển một số giao diện HTML thành giao diện JSX.
60
60
61
61
<Note>
62
62
63
-
JSX and React are two separate things. They're often used together, but you *can*[use them independently](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#whats-a-jsx-transform) of each other. JSX is a syntax extension, while React is a JavaScript library.
63
+
JSX và React là hai thứ riêng biệt. Chúng thường được sử dụng cùng nhau, nhưng bạn *có thể*[sử dụng chúng một cách độc lập](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#whats-a-jsx-transform).JSX là một cú pháp mở rộng, trong khi React là thư viện JavaScript.
64
64
65
65
</Note>
66
66
67
-
## Converting HTML to JSX {/*converting-html-to-jsx*/}
67
+
## Chuyển HTML thành JSX {/*converting-html-to-jsx*/}
68
68
69
-
Suppose that you have some (perfectly valid) HTML:
69
+
Giả sử bạn có một đoạn HTML (hoàn toàn hợp lệ):
70
70
71
71
```html
72
72
<h1>Hedy Lamarr's Todos</h1>
@@ -82,7 +82,7 @@ Suppose that you have some (perfectly valid) HTML:
82
82
</ul>
83
83
```
84
84
85
-
And you want to put it into your component:
85
+
Và bạn muốn đặt nó vào component:
86
86
87
87
```js
88
88
exportdefaultfunctionTodoList() {
@@ -92,7 +92,7 @@ export default function TodoList() {
92
92
}
93
93
```
94
94
95
-
If you copy and paste it as is, it will not work:
95
+
Nếu bạn sao chép dùng như vậy, nó sẽ không hoạt động:
96
96
97
97
98
98
<Sandpack>
@@ -122,21 +122,21 @@ img { height: 90px }
122
122
123
123
</Sandpack>
124
124
125
-
This is because JSX is stricter and has a few more rules than HTML! If you read the error messages above, they'll guide you to fix the markup, or you can follow the guide below.
125
+
Điều này xảy ra do JSX chặt chẽ hơn và có nhiều quy tắc hơn HTML! Nếu bạn đọc các thông báo lỗi ở trên, nó sẽ hướng dẫn bạn sửa lỗi giao diện hoặc bạn có thể làm theo hướng dẫn bên dưới.
126
126
127
127
<Note>
128
128
129
-
Most of the time, React's on-screen error messages will help you find where the problem is. Give them a read if you get stuck!
129
+
Trong hầu hết các trường hợp, thông báo lỗi trên màn hình của React sẽ giúp bạn tìm ra vấn đề ở chỗ nào. Hãy đọc chúng nếu bạn gặp khó khăn!
130
130
131
131
</Note>
132
132
133
-
## The Rules of JSX {/*the-rules-of-jsx*/}
133
+
## Các quy tắc JSX {/*the-rules-of-jsx*/}
134
134
135
-
### 1. Return a single root element {/*1-return-a-single-root-element*/}
135
+
### 1. Trả về một root element {/*1-return-a-single-root-element*/}
136
136
137
-
To return multiple elements from a component, **wrap them with a single parent tag.**
137
+
Trả về nhiều elements từ một component, **wrap them with a single parent tag.**
138
138
139
-
For example, you can use a `<div>`:
139
+
Ví dụ, bạn có thể sử dụng `<div>`:
140
140
141
141
```js {1,11}
142
142
<div>
@@ -153,7 +153,7 @@ For example, you can use a `<div>`:
153
153
```
154
154
155
155
156
-
If you don't want to add an extra `<div>`to your markup, you can write `<>`and`</>`instead:
156
+
Nếu bạn không muốn thêm một thẻ `<div>`vào giao diện, bạn có thể viết `<>`và`</>`để thay thế:
157
157
158
158
```js {1,11}
159
159
<>
@@ -169,21 +169,21 @@ If you don't want to add an extra `<div>` to your markup, you can write `<>` and
169
169
</>
170
170
```
171
171
172
-
This empty tag is called a *[Fragment.](/reference/react/Fragment)* Fragments let you group things without leaving any trace in the browser HTML tree.
172
+
Thẻ trống này được gọi là *[Fragment.](/reference/react/Fragment)* Fragments giúp bạn nhóm các elements mà không để lại bất kỳ dấu vết nào trong cây HTML của trình duyệt.
173
173
174
174
<DeepDive>
175
175
176
-
#### Why do multiple JSX tags need to be wrapped? {/*why-do-multiple-jsx-tags-need-to-be-wrapped*/}
176
+
#### Tại sao nhiều thẻ JSX cần được bọc {/*why-do-multiple-jsx-tags-need-to-be-wrapped*/}
177
177
178
-
JSX looks like HTML, but under the hood it is transformed into plain JavaScript objects. You can't return two objects from a function without wrapping them into an array. This explains why you also can't return two JSX tags without wrapping them into another tag or a Fragment.
178
+
JSX trông giống như HTML, nhưng bên dưới nó được chuyển đổi thành các đối tượng JavaScript đơn giản. Bạn không thể trả về hai đối tượng từ một hàm mà không để chúng vào một mảng. Điều này giải thích tại sao bạn cũng không thể trả về hai thẻ JSX mà không để chúng vào một thẻ khác hoặc một Fragment.
179
179
180
180
</DeepDive>
181
181
182
-
### 2. Close all the tags {/*2-close-all-the-tags*/}
182
+
### 2. Đóng tất cả các thẻ {/*2-close-all-the-tags*/}
183
183
184
-
JSX requires tags to be explicitly closed: self-closing tags like `<img>` must become `<img />`, and wrapping tags like `<li>oranges` must be written as `<li>oranges</li>`.
184
+
JSX yêu cầu các thẻ phải được đóng rõ ràng: các thẻ tự đóng như `<img>` phải trở thành `<img />` và các thẻ bọc như `<li>oranges` phải được viết là `<li>oranges</li>`.
185
185
186
-
This is how Hedy Lamarr's image and list items look closed:
186
+
Image và danh sách các mục của Hedy Lamarr trông như sau:
187
187
188
188
```js {2-6,8-10}
189
189
<>
@@ -200,11 +200,11 @@ This is how Hedy Lamarr's image and list items look closed:
200
200
</>
201
201
```
202
202
203
-
### 3. camelCase <s>all</s> most of the things! {/*3-camelcase-salls-most-of-the-things*/}
JSX turns into JavaScript and attributes written in JSX become keys of JavaScript objects. In your own components, you will often want to read those attributes into variables. But JavaScript has limitations on variable names. For example, their names can't contain dashes or be reserved words like `class`.
205
+
JSX chuyển thành JavaScript và các thuộc tính được viết bằng JSX trở thành khóa của các đối tượng JavaScript. Trong components của mình, bạn thường muốn đưa các thuộc tính đó thành các biến. Nhưng JavaScript có những hạn chế về tên biến. Ví dụ: tên của chúng không được chứa dấu gạch ngang hoặc các từ dành riêng như `class`.
206
206
207
-
This is why, in React, many HTML and SVG attributes are written in camelCase. For example, instead of `stroke-width` you use `strokeWidth`. Since `class` is a reserved word, in React you write `className` instead, named after the [corresponding DOM property](https://developer.mozilla.org/en-US/docs/Web/API/Element/className):
207
+
Đây là lý do tại sao trong React, nhiều thuộc tính HTML và SVG được viết bằng CamelCase. Ví dụ: thay vì `stroke-width` bạn sử dụng `strokeWidth`. Vì `class` là một từ dành riêng, nên trong React bạn viết `className`, được đặt tên theo [thuộc tính DOM tương ứng](https://developer.mozilla.org/en-US/docs/Web/API/Element/className):
208
208
209
209
```js {4}
210
210
<img
@@ -215,18 +215,19 @@ This is why, in React, many HTML and SVG attributes are written in camelCase. Fo
215
215
```
216
216
217
217
You can [find all these attributes in the list of DOM component props.](/reference/react-dom/components/common) If you get one wrong, don't worry—React will print a message with a possible correction to the [browser console.](https://developer.mozilla.org/docs/Tools/Browser_Console)
218
+
Bạn có thể [tìm tất cả các thuộc tính này trong danh sách các DOM component props.](/reference/react-dom/comComponents/common) Nếu bạn có một thuộc tính sai, đừng lo lắng—React sẽ đưa ra một thông báo có thể sửa lỗi trên [console của trình duyệt.](https://developer.mozilla.org/docs/Tools/Browser_Console)
218
219
219
220
<Pitfall>
220
221
221
-
For historical reasons, [`aria-*`](https://developer.mozilla.org/docs/Web/Accessibility/ARIA) and [`data-*`](https://developer.mozilla.org/docs/Learn/HTML/Howto/Use_data_attributes) attributes are written as in HTML with dashes.
222
+
Vì lý do lịch sử, [`aria-*`](https://developer.mozilla.org/docs/Web/Accessibility/ARIA) và [`data-*`](https://developer.mozilla.org/docs/Learn/HTML/Howto/Use_data_attributes) được viết dưới dạng HTML với dấu gạch ngang.
222
223
223
224
</Pitfall>
224
225
225
-
### Pro-tip: Use a JSX Converter {/*pro-tip-use-a-jsx-converter*/}
226
+
### Pro-tip:Sử dụng Trình chuyển đổi JSX {/*pro-tip-use-a-jsx-converter*/}
226
227
227
-
Converting all these attributes in existing markup can be tedious! We recommend using a [converter](https://transform.tools/html-to-jsx) to translate your existing HTML and SVG to JSX. Converters are very useful in practice, but it'sstillworthunderstandingwhatisgoingonsothatyoucancomfortablywriteJSXonyourown.
228
+
Việc chuyển đổi tất cả các thuộc tính này trong giao diện hiện tại có thể rất tẻ nhạt! Chúng tôi khuyên bạn nên sử dụng [trình chuyển đổi](https://transform.tools/html-to-jsx) để dịch HTML và SVG hiện có sang JSX. Các trình chuyển đổi rất hữu ích trong thực tế, nhưng vẫn đáng để hiểu những gì đang diễn ra khi đó bạn có thể viết JSX một cách thoải mái nhất.
Copy file name to clipboardExpand all lines: src/content/learn/your-first-component.md
+1-5Lines changed: 1 addition & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -211,11 +211,7 @@ Khi mà một component con cần dữ liệu từ cha, [hãy truyền nó qua p
211
211
212
212
#### Sử dụng Component từ đầu đến cuối {/*components-all-the-way-down*/}
213
213
214
-
<<<<<<< HEAD
215
-
Ứng dụng React của bạn bắt đầu từ một component "gốc" (root). Thường thì component này được tạo ra tự động khi mà bạn bắt đầu một dự án mới. Ví dụ, nếu bạn dùng [CodeSandbox](https://codesandbox.io/) hay [Create React App](https://create-react-app.dev/), component gốc sẽ được định nghĩa ở `src/App.js`. Nếu bạn sử dụng framework [Next.js](https://nextjs.org/), component gốc sẽ được định nghĩa ở `pages/index.js`. Trong những ví dụ này, bạn đã export các component gốc.
216
-
=======
217
-
Your React application begins at a "root" component. Usually, it is created automatically when you start a new project. For example, if you use [CodeSandbox](https://codesandbox.io/) or if you use the framework [Next.js](https://nextjs.org/), the root component is defined in `pages/index.js`. In these examples, you've been exporting root components.
218
-
>>>>>>> 7bdbab144e09d4edf793ff5128080eb1dba79be4
214
+
Ứng dụng React của bạn bắt đầu từ một component "gốc" (root). Thường thì component này được tạo ra tự động khi mà bạn bắt đầu một dự án mới. Ví dụ, nếu bạn dùng [CodeSandbox](https://codesandbox.io/) hay nếu bạn sử dụng framework [Next.js](https://nextjs.org/), component gốc sẽ được định nghĩa ở `pages/index.js`. Trong những ví dụ này, bạn đã export các component gốc.
219
215
220
216
Hầu hết các ứng dụng React đều sử dụng component từ đầu đến cuối. Điều này có nghĩa là bạn không chỉ sử dụng component cho những phần có thể tái sử dụng như các nút (button) mà còn cho các phần lớn hơn như sidebar, các danh sách và đích đến cuối, các trang hoàn chỉnh! Component là một cách tiện lợi để tổ chức code UI và markup, ngay cả khi một số chúng chỉ được sử dụng một lần.
0 commit comments