Skip to content

Translate Introducing JSX #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 41 additions & 41 deletions content/docs/introducing-jsx.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
---
id: introducing-jsx
title: Introducing JSX
title: Giới thiệu JSX
permalink: docs/introducing-jsx.html
prev: hello-world.html
next: rendering-elements.html
---

Consider this variable declaration:
Xem xét khai báo biến dưới đây:

```js
const element = <h1>Hello, world!</h1>;
```

This funny tag syntax is neither a string nor HTML.
Cú pháp thẻ này không phải là một chuỗi kí tự cũng không phải là một thẻ HTML

It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript.
Nó được gọi là JSX, là một cú pháp mở rộng cho JavaScript. Chúng tôi khuyến khích sử dụng JSX với React để mô tả giao diện (UI). JSX có thể trông giống Ngôn ngữ Khuôn mẫu (Template language), nhưng JSX đi kèm với toàn bộ tính năng của JavaScript.

JSX produces React "elements". We will explore rendering them to the DOM in the [next section](/docs/rendering-elements.html). Below, you can find the basics of JSX necessary to get you started.
JSX cho ra những "phần tử"(Element) React. Chúng ta sẽ khám phá việc chúng được render vào DOM như thế nào ở [phần tiếp theo](/docs/rendering-elements.html). Dưới đây là những kiến thức cần thiết cơ bản của JSX để có thể bắt đầu.

### Why JSX? {#why-jsx}
### Tại sao lại là JSX? {#why-jsx}

React embraces the fact that rendering logic is inherently coupled with other UI logic: how events are handled, how the state changes over time, and how the data is prepared for display.
React khuyến khích một thực tế là logic của render được kết hợp một cách tự nhiên với những logic khác liên quan tới UI: Các sự kiện được xử lý như thế nào, state thay đổi như thế nào theo thời gian và dữ liệu được chuẩn bị như thế nào cho việc hiển thị.

Instead of artificially separating *technologies* by putting markup and logic in separate files, React [separates *concerns*](https://en.wikipedia.org/wiki/Separation_of_concerns) with loosely coupled units called "components" that contain both. We will come back to components in a [further section](/docs/components-and-props.html), but if you're not yet comfortable putting markup in JS, [this talk](https://www.youtube.com/watch?v=x7cQ3mrcKaY) might convince you otherwise.
Thay vì tách biệt các *công nghệ* một cách giả tạo bằng cách đưa định nghĩa giao diện và logic vào những tệp khác nhau, React [tách bạch *mối quan hệ*](https://en.wikipedia.org/wiki/Separation_of_concerns) bằng những đơn vị rời rạc gọi là "components" chứa cả hai cái trên. Chúng ta sẽ bàn về components trong một [phần khác](/docs/components-and-props.html), nếu bạn vẫn chưa cảm thấy thoải mái việc đưa các định nghĩa giao diện vào trong JS, [buổi nói chuyện này](https://www.youtube.com/watch?v=x7cQ3mrcKaY) có thể thuyết phục bạn.

React [doesn't require](/docs/react-without-jsx.html) using JSX, but most people find it helpful as a visual aid when working with UI inside the JavaScript code. It also allows React to show more useful error and warning messages.
React [Không bắt buộc](/docs/react-without-jsx.html) sử dụng JSX, nhưng phần lớn mọi người đều cho rằng nó hữu dụng khi làm việc với giao diện (UI) trong mã JavaScript. JSX cũng cho phép React hiển thị những thông báo lỗi và "lời cảnh báo" (warning) hữu ích hơn.

With that out of the way, let's get started!
Vậy bắt đầu thôi!

### Embedding Expressions in JSX {#embedding-expressions-in-jsx}
### Nhúng Biểu thức trong JSX {#embedding-expressions-in-jsx}

In the example below, we declare a variable called `name` and then use it inside JSX by wrapping it in curly braces:
Ở ví dụ dưới, chúng ta khai báo một biến tên là `name` và dùng bên trong JSX bằng cách bao trong cặp dấu ngoặc nhọn:

```js{1,2}
const name = 'Josh Perez';
Expand All @@ -42,9 +42,9 @@ ReactDOM.render(
);
```

You can put any valid [JavaScript expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Expressions) inside the curly braces in JSX. For example, `2 + 2`, `user.firstName`, or `formatName(user)` are all valid JavaScript expressions.
Bạn có thể nhúng bất kỳ [biểu thức JavaScript hợp lệ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Expressions) bên trong JSX bằng cặp dấu ngoặc nhọn. Ví dụ, `2 + 2`, `user.firstName`, hoặc `formatName(user)` đều là các biểu thức hợp lệ của JavaScript.

In the example below, we embed the result of calling a JavaScript function, `formatName(user)`, into an `<h1>` element.
Ở ví dụ dưới, chúng ta nhúng kết quả của một hàm JavaScript, `formatName(user)`, vào bên trong phần tử `<h1>`.

```js{12}
function formatName(user) {
Expand All @@ -70,13 +70,13 @@ ReactDOM.render(

[](codepen://introducing-jsx)

We split JSX over multiple lines for readability. While it isn't required, when doing this, we also recommend wrapping it in parentheses to avoid the pitfalls of [automatic semicolon insertion](http://stackoverflow.com/q/2846283).
Chúng ta có thể tách JSX vào những tệp khác nhau cho dễ đọc. Việc này không bắt buộc, nhưng khi làm như vậy, chúng tôi cũng khuyến khích nhúng trong cặp dấu ngoặc để tránh rơi vào trường hợp JS Engines [tự thêm chấm phẩy](http://stackoverflow.com/q/2846283).

### JSX is an Expression Too {#jsx-is-an-expression-too}
### JSX cũng là một biểu thức {#jsx-is-an-expression-too}

After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.
Sau khi biên dịch (compile), biểu thức JSX là những gọi hàm bình thường của JavaScript và thành những đối tượng JavaScript sau khi được gọi.

This means that you can use JSX inside of `if` statements and `for` loops, assign it to variables, accept it as arguments, and return it from functions:
Điều này có nghĩa là bạn có thể dùng JSX bên trong câu lệnh `if` cũng như vòng lặp `for`, gán nó cho biến, dùng như tham số hàm, và trả về JSX từ hàm.

```js{3,5}
function getGreeting(user) {
Expand All @@ -87,37 +87,37 @@ function getGreeting(user) {
}
```

### Specifying Attributes with JSX {#specifying-attributes-with-jsx}
### Xác định thuộc tính của thẻ với JSX {#specifying-attributes-with-jsx}

You may use quotes to specify string literals as attributes:
Bạn có thể dùng dấu nháy để khai báo một chuỗi như là thuộc tính của thẻ:

```js
const element = <div tabIndex="0"></div>;
```

You may also use curly braces to embed a JavaScript expression in an attribute:
Bạn có thể dùng dấu ngoặc nhọn để nhúng một biểu thức Javascript vào trong thuộc tính:

```js
const element = <img src={user.avatarUrl}></img>;
```

Don't put quotes around curly braces when embedding a JavaScript expression in an attribute. You should either use quotes (for string values) or curly braces (for expressions), but not both in the same attribute.
Đừng để dấu nháy xung quanh ngoặc nhọn khi nhúng biểu thức JavaScript vào trong một thuộc tính. Bạn có thể dùng dấu nháy (cho giá trị chuỗi) hoặc ngoặc nhọn (cho biểu thức), nhưng không được dùng cả hai cho cùng 1 thuộc tính.

>**Warning:**
>**CẢNH BÁO:**
>
>Since JSX is closer to JavaScript than to HTML, React DOM uses `camelCase` property naming convention instead of HTML attribute names.
> JSX gần với JavaScript hơn là so với HTML, React DOM sử dụng chuẩn quy tắc đặt tên `camelCase` cho thuộc tính thay vì dùng tên thuộc tính gốc của HTML.
>
>For example, `class` becomes [`className`](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) in JSX, and `tabindex` becomes [`tabIndex`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/tabIndex).
>Ví dụ, `class` trở thành [`className`](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) trong JSX, `tabindex` trở thành [`tabIndex`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/tabIndex).

### Specifying Children with JSX {#specifying-children-with-jsx}
### Dùng thẻ con trong JSX {#specifying-children-with-jsx}

If a tag is empty, you may close it immediately with `/>`, like XML:
Nếu tag rỗng (không có thẻ con), bạn có thể đóng nó ngay lập tức với `/>`, giống XML:

```js
const element = <img src={user.avatarUrl} />;
```

JSX tags may contain children:
Thẻ JSX có thể chứa thẻ con:

```js
const element = (
Expand All @@ -128,23 +128,23 @@ const element = (
);
```

### JSX Prevents Injection Attacks {#jsx-prevents-injection-attacks}
### JSX chống tấn công kiểu Injection {#jsx-prevents-injection-attacks}

It is safe to embed user input in JSX:
Việc nhúng đầu vào của người dùng trong JSX là an toàn:

```js
const title = response.potentiallyMaliciousInput;
// This is safe:
// Việc này an toàn:
const element = <h1>{title}</h1>;
```

By default, React DOM [escapes](http://stackoverflow.com/questions/7381974/which-characters-need-to-be-escaped-on-html) any values embedded in JSX before rendering them. Thus it ensures that you can never inject anything that's not explicitly written in your application. Everything is converted to a string before being rendered. This helps prevent [XSS (cross-site-scripting)](https://en.wikipedia.org/wiki/Cross-site_scripting) attacks.
Mặc định, React DOM [loại bỏ những kí tự đặc biệt](http://stackoverflow.com/questions/7381974/which-characters-need-to-be-escaped-on-html) ở bên trong bất kì giá trị nào được nhúng vào JSX trước khi render chúng. Việc này đảm bảo không có giá trị xấu nào được vô tình được đưa vào ứng dụng. Mọi thứ đều được chuyển thành chuỗi trước khi được render. Việc này giúp ngăn chặn phương thức [tấn công XSS (cross-site-scripting)](https://en.wikipedia.org/wiki/Cross-site_scripting).

### JSX Represents Objects {#jsx-represents-objects}
### JSX là đối tượng {#jsx-represents-objects}

Babel compiles JSX down to `React.createElement()` calls.
Babel biên dịch JSX thành những câu gọi hàm `React.createElement()`.

These two examples are identical:
Hai ví dụ dưới đây là tương tự:

```js
const element = (
Expand All @@ -162,10 +162,10 @@ const element = React.createElement(
);
```

`React.createElement()` performs a few checks to help you write bug-free code but essentially it creates an object like this:
`React.createElement()` thực hiện một số kiểm tra để giúp bạn viết mã không bị lỗi nhưng cơ bản nó tạo một đối tượng giống thế này:

```js
// Note: this structure is simplified
// Lưu ý: cấu trúc nãy đã được đơn giản hoá
const element = {
type: 'h1',
props: {
Expand All @@ -175,10 +175,10 @@ const element = {
};
```

These objects are called "React elements". You can think of them as descriptions of what you want to see on the screen. React reads these objects and uses them to construct the DOM and keep it up to date.
Những đối tượng này được gọi là "Những phần tử React" (React elements). Bạn có thể xem chúng như là mô tả những gì bạn muốn thấy trên màn hình. React đọc những đối tượng này và dùng chúng để xây dựng DOM và cập nhật nó.

We will explore rendering React elements to the DOM in the next section.
Chúng ta sẽ khám phá cách các React Element được render vào DOM thế nào trong phần kế tiếp.

>**Tip:**
>**Gợi ý:**
>
>We recommend using the ["Babel" language definition](http://babeljs.io/docs/editors) for your editor of choice so that both ES6 and JSX code is properly highlighted. This website uses the [Oceanic Next](https://labs.voronianski.com/oceanic-next-color-scheme/) color scheme which is compatible with it.
>Chúng tôi khuyến khích sử dụng [Định nghĩa ngôn ngữ "Babel"](http://babeljs.io/docs/editors) cho trình soạn thảo của bạn như vậy cả mã ES6 JSX đều có thể được tô sáng rõ ràng. Trang web này dùng tập màu [Oceanic Next](https://labs.voronianski.com/oceanic-next-color-scheme/) cái mà phù hợp cho việc này.