Skip to content

Commit

Permalink
Merge branch 'main' into vn-quick-start-page
Browse files Browse the repository at this point in the history
  • Loading branch information
ltmylinh committed Apr 16, 2024
2 parents 09e17e9 + d37c4a5 commit e576ec6
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 186 deletions.
5 changes: 0 additions & 5 deletions src/content/community/meetups.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,8 @@ Bạn đã có React.js meetup tại quốc gia của bạn chưa? Thêm nó và
* [Kuala Lumpur](https://www.kl-react.com/)
* [Penang](https://www.facebook.com/groups/reactpenang/)

<<<<<<< HEAD
## Hà Lan {/*netherlands*/}
* [Amsterdam](https://www.meetup.com/React-Amsterdam/)
=======
## Netherlands {/*netherlands*/}
* [Amsterdam](https://guild.host/react-amsterdam)
>>>>>>> 7bdbab144e09d4edf793ff5128080eb1dba79be4

## New Zealand {/*new-zealand*/}
* [Wellington](https://www.meetup.com/React-Wellington/)
Expand Down
86 changes: 43 additions & 43 deletions src/content/learn/add-react-to-an-existing-project.md

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions src/content/learn/state-as-a-snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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.

<<<<<<< HEAD
Khi React re-render một component:
1. React gọi lại hàm của bạn.
2. Hàm của bạn trả về một snapshot JSX mới.
3. React cập nhật màn hình sao cho tương đồng với snapshot bạn đã trả về.
=======
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.

When React re-renders a component:

1. React calls your function again.
2. Your function returns a new JSX snapshot.
3. React then updates the screen to match the snapshot your function returned.
>>>>>>> 7bdbab144e09d4edf793ff5128080eb1dba79be4

<IllustrationBlock sequential>
<Illustration caption="React thực thi hàm của bạn" src="/images/docs/illustrations/i_render1.png" />
Expand Down
90 changes: 44 additions & 46 deletions src/content/learn/writing-markup-with-jsx.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
---
title: Writing Markup with JSX
title: Viết Giao Diện Với JSX
---

<Intro>

*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.
*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.

</Intro>

<YouWillLearn>

* Why React mixes markup with rendering logic
* How JSX is different from HTML
* How to display information with JSX
* Tại sao React kết hợp giao diện với rendering logic
* JSX khác HTML như thế nào
* Hiển thị thông tin với JSX như thế nào

</YouWillLearn>

## JSX: Putting markup into JavaScript {/*jsx-putting-markup-into-javascript*/}
## JSX: Đưa giao diện vào JavaScript {/*jsx-putting-markup-into-javascript*/}

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:
Web đã được xây dựng dựa trên HTML, CSS 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:

<DiagramGroup>

<Diagram name="writing_jsx_html" height={237} width={325} alt="HTML markup with purple background and a div with two child tags: p and form. ">
<Diagram name="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 form.">

HTML

</Diagram>

<Diagram name="writing_jsx_js" height={237} width={325} alt="Three JavaScript handlers with yellow background: onSubmit, onLogin, and onClick.">
<Diagram name="writing_jsx_js" height={237} width={325} alt="Ba trình xử lý JavaScript có nền màu vàng: onSubmit, onLogin onClick.">

JavaScript

</Diagram>

</DiagramGroup>

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.**
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.**

<DiagramGroup>

Expand All @@ -54,19 +54,19 @@ But as the Web became more interactive, logic increasingly determined content. J

</DiagramGroup>

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.
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.

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.
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.

<Note>

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.
JSX 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.

</Note>

## Converting HTML to JSX {/*converting-html-to-jsx*/}
## Chuyển HTML thành JSX {/*converting-html-to-jsx*/}

Suppose that you have some (perfectly valid) HTML:
Giả sử bạn có một đoạn HTML (hoàn toàn hợp lệ):

```html
<h1>Hedy Lamarr's Todos</h1>
Expand All @@ -82,7 +82,7 @@ Suppose that you have some (perfectly valid) HTML:
</ul>
```

And you want to put it into your component:
Và bạn muốn đặt nó vào component:

```js
export default function TodoList() {
Expand All @@ -92,7 +92,7 @@ export default function TodoList() {
}
```

If you copy and paste it as is, it will not work:
Nếu bạn sao chép dùng như vậy, nó sẽ không hoạt động:


<Sandpack>
Expand Down Expand Up @@ -122,21 +122,21 @@ img { height: 90px }
</Sandpack>
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.
Đ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.
<Note>
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!
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!
</Note>
## The Rules of JSX {/*the-rules-of-jsx*/}
## Các quy tắc JSX {/*the-rules-of-jsx*/}
### 1. Return a single root element {/*1-return-a-single-root-element*/}
### 1. Trả về một root element {/*1-return-a-single-root-element*/}
To return multiple elements from a component, **wrap them with a single parent tag.**
Trả về nhiều elements từ một component, **wrap them with a single parent tag.**
For example, you can use a `<div>`:
Ví dụ, bạn có thể sử dụng `<div>`:
```js {1,11}
<div>
Expand All @@ -153,7 +153,7 @@ For example, you can use a `<div>`:
```
If you don't want to add an extra `<div>` to your markup, you can write `<>` and `</>` instead:
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 `<>` `</>` để thay thế:
```js {1,11}
<>
Expand All @@ -169,21 +169,21 @@ If you don't want to add an extra `<div>` to your markup, you can write `<>` and
</>
```
This empty tag is called a *[Fragment.](/reference/react/Fragment)* Fragments let you group things without leaving any trace in the browser HTML tree.
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.
<DeepDive>
#### Why do multiple JSX tags need to be wrapped? {/*why-do-multiple-jsx-tags-need-to-be-wrapped*/}
#### Tại sao nhiều thẻ JSX cần được bọc {/*why-do-multiple-jsx-tags-need-to-be-wrapped*/}
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.
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.
</DeepDive>
### 2. Close all the tags {/*2-close-all-the-tags*/}
### 2. Đóng tất cả các thẻ {/*2-close-all-the-tags*/}
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>`.
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> `.
This is how Hedy Lamarr's image and list items look closed:
Image và danh sách các mục của Hedy Lamarr trông như sau:
```js {2-6,8-10}
<>
Expand All @@ -200,11 +200,11 @@ This is how Hedy Lamarr's image and list items look closed:
</>
```
### 3. camelCase <s>all</s> most of the things! {/*3-camelcase-salls-most-of-the-things*/}
### 3. camelCase <s>tất cả</s> hầu hết mọi thứ! {/*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`.
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`.
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):
Đây là lý do tại sao trong React, nhiều thuộc tính HTML SVG được viết bằng CamelCase. Ví dụ: thay vì `stroke-width` bạn sử dụng `strokeWidth`. `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):
```js {4}
<img
Expand All @@ -215,18 +215,19 @@ This is why, in React, many HTML and SVG attributes are written in camelCase. Fo
```
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)
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)

<Pitfall>

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.
Vì lý do lịch sử, [`aria-*`](https://developer.mozilla.org/docs/Web/Accessibility/ARIA) [`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.

</Pitfall>

### Pro-tip: Use a JSX Converter {/*pro-tip-use-a-jsx-converter*/}
### Pro-tip: Sử dụng Trình chuyển đổi JSX {/*pro-tip-use-a-jsx-converter*/}

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's still worth understanding what is going on so that you can comfortably write JSX on your own.
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 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.

Here is your final result:
Đây là kết quả cuối cùng:

<Sandpack>

Expand Down Expand Up @@ -258,21 +259,18 @@ img { height: 90px }

<Recap>

Now you know why JSX exists and how to use it in components:

* React components group rendering logic together with markup because they are related.
* JSX is similar to HTML, with a few differences. You can use a [converter](https://transform.tools/html-to-jsx) if you need to.
* Error messages will often point you in the right direction to fixing your markup.

* Nhóm React components rendering logic cùng với giao diện vì chúng có liên quan với nhau.
* JSX tương tự như HTML, có một số điểm khác biệt. Bạn có thể sử dụng [bộ chuyển đổi](https://transform.tools/html-to-jsx) nếu cần.
* Thông báo lỗi thường sẽ chỉ cho bạn hướng đi đúng để sửa giao diện của mình.
</Recap>



<Challenges>

#### Convert some HTML to JSX {/*convert-some-html-to-jsx*/}
#### Chuyển đổi một số HTML sang JSX {/*convert-some-html-to-jsx*/}

This HTML was pasted into a component, but it's not valid JSX. Fix it:
HTML này đã được dán vào một component nhưng nó không phải là JSX hợp lệ. Sửa nó:

<Sandpack>

Expand Down Expand Up @@ -308,7 +306,7 @@ export default function Bio() {

</Sandpack>

Whether to do it by hand or using the converter is up to you!
Việc thực hiện bằng tay hay sử dụng công cụ chuyển đổi là tùy thuộc vào bạn!

<Solution>

Expand Down
6 changes: 1 addition & 5 deletions src/content/learn/your-first-component.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,7 @@ Khi mà một component con cần dữ liệu từ cha, [hãy truyền nó qua p

#### Sử dụng Component từ đầu đến cuối {/*components-all-the-way-down*/}

<<<<<<< HEAD
Ứ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.
=======
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.
>>>>>>> 7bdbab144e09d4edf793ff5128080eb1dba79be4
Ứ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.

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.

Expand Down
Loading

0 comments on commit e576ec6

Please sign in to comment.