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/reference-dom-elements.md
+36-32Lines changed: 36 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -14,27 +14,27 @@ redirect_from:
14
14
- "tips/dangerously-set-inner-html.html"
15
15
---
16
16
17
-
React implements a browser-independent DOM system for performance and cross-browser compatibility. We took the opportunity to clean up a few rough edges in browser DOM implementations.
17
+
React áp dụng hệ thống DOM không phụ thuộc vào trình duyệt để tăng hiệu suất và độ tương thích với nhiều trình duyệt khác nhau. Nhân dịp này, chúng tôi đã chỉnh sửa một vài điểm không tương đồng trong cách làm việc với DOM của các trình duyệt.
18
18
19
-
In React, all DOM properties and attributes (including event handlers) should be camelCased. For example, the HTML attribute `tabindex` corresponds to the attribute`tabIndex` in React. The exception is `aria-*`and`data-*`attributes, which should be lowercased. For example, you can keep `aria-label`as`aria-label`.
19
+
Trong React, thuộc tính DOM va các sự kiện (properties và attributes, vd như id, class, value, data-attr, onclick, ...) phải được viết theo camelCase. Ví dụ như attribute `tabindex`, thì trong React là`tabIndex`. Có những ngoại lệ là thuộc tính `aria-*`và`data-*`phải được viết chữ thường. Ví dụ `aria-label`vẫn là`aria-label`.
20
20
21
-
## Differences In Attributes {#differences-in-attributes}
21
+
## Sự khác biệt trong các thuộc tính {#differences-in-attributes}
22
22
23
-
There are a number of attributes that work differently between React and HTML:
23
+
Có một số sự khác biệt trong cách thuộc tính hoạt động trong React so với HTML:
24
24
25
25
### checked {#checked}
26
26
27
-
The `checked`attribute is supported by `<input>`components of type `checkbox`or`radio`. You can use it to set whether the component is checked. This is useful for building controlled components. `defaultChecked`is the uncontrolled equivalent, which sets whether the component is checked when it is first mounted.
27
+
Thuộc tính `checked`trong các `<input>`có type là `checkbox`và`radio` được dùng để tạo controlled components, xem mục Controlled Components để biết thêm. `defaultChecked`được dùng để đặt giá trị ban đầu của input, được dùng để tạo Uncontrolled Components.
28
28
29
29
### className {#classname}
30
30
31
-
To specify a CSS class, use the `className` attribute. This applies to all regular DOM and SVG elements like `<div>`, `<a>`, and others.
31
+
Để đặt CSS class cho các thẻ DOM hay SVG như `div`, `a`, ..., chúng ta sử dụng `className` thay vì `class` như thường lệ do bị đụng với từ khoá của ES6 class.
32
32
33
-
If you use React with Web Components (which is uncommon), use the `class` attribute instead.
33
+
Nếu bạn sử dụng Web Components (trường hợp không phổ biến), thì vẫn dùng `class`.
`dangerouslySetInnerHTML`is React's replacement for using `innerHTML`in the browser DOM. In general, setting HTML from code is risky because it's easy to inadvertently expose your users to a [cross-site scripting (XSS)](https://en.wikipedia.org/wiki/Cross-site_scripting) attack. So, you can set HTML directly from React, but you have to type out `dangerouslySetInnerHTML`and pass an object with a `__html` key, to remind yourself that it's dangerous. For example:
37
+
`dangerouslySetInnerHTML`tương đương với `innerHTML`trong DOM. Nhìn chung, việc thay đổi DOM từ Javascript nguy hiểm do mình có thể vô tình để người dùng vô tình bị tấn công bởi [cross-site scripting (XSS)](https://en.wikipedia.org/wiki/Cross-site_scripting). Vì vậy, trong React, bạn phải sử dụng `dangerouslySetInnerHTML`và truyền một object với key là `_html` để đề phòng. Ví dụ:
38
38
39
39
```js
40
40
functioncreateMarkup() {
@@ -48,23 +48,25 @@ function MyComponent() {
48
48
49
49
### htmlFor {#htmlfor}
50
50
51
-
Since`for`is a reserved word in JavaScript, React elements use `htmlFor` instead.
51
+
Do`for`là một từ khoá trong Javascript, React dùng `htmlFor`.
52
52
53
53
### onChange {#onchange}
54
54
55
-
The `onChange`event behaves as you would expect it to: whenever a form field is changed, this event is fired. We intentionally do not use the existing browser behavior because `onChange` is a misnomer for its behavior and React relies on this event to handle user input in real time.
55
+
Sự kiện `onChange`hoạt động đúng như tên gọi của nó, được phát ra khi giá trị của `<input>` thay đổi. React dựa vào sự kiện này để xử lý đầu vào từ người dùng ở thời gian thực.
56
56
57
57
### selected {#selected}
58
58
59
59
The `selected` attribute is supported by `<option>` components. You can use it to set whether the component is selected. This is useful for building controlled components.
60
60
61
+
Thuộc tính `selected` được sử dụng trong `<option>` để đánh dấu option nào được chọn trong một `<select>` cho Controlled component.
62
+
61
63
### style {#style}
62
64
63
-
>Note
65
+
>Ghi chú
64
66
>
65
-
>Some examples in the documentation use `style`for convenience, but **using the `style`attribute as the primary means of styling elements is generally not recommended.**In most cases, [`className`](#classname)should be used to reference classes defined in an external CSS stylesheet. `style`is most often used in React applications to add dynamically-computed styles at render time. See also[FAQ: Styling and CSS](/docs/faq-styling.html).
67
+
>Một vài ví dụ trong tài liệu này sử dụng `style`cho tiện, thực tế **sử dụng thuộc tính `style`tại chỗ không được khuyến khích .**Trong đa phần các trường hợp, [`className`](#classname)nên được dùng cùng với một file CSS rời để style. Thuộc tính `style`thường được dùng trong React để style động ví dụ như người dùng thay đổi màu hoặc font-size bằng một input. Xem thêm[FAQ: Styling and CSS](/docs/faq-styling.html).
66
68
67
-
The `style`attribute accepts a JavaScript object with camelCased properties rather than a CSS string. This is consistent with the DOM `style` JavaScript property, is more efficient, and prevents XSS security holes. For example:
69
+
Thuộc tính `style`nhận vào một object với các thuộc tính CSS ở dạng camelCase. Viết bằng camelCase để tích hợp với key của object trong Javascript, đồng thời mang lại sự nhất quán và chống tấn công XSS. Ví dụ:
68
70
69
71
```js
70
72
constdivStyle= {
@@ -77,22 +79,22 @@ function HelloWorldComponent() {
77
79
}
78
80
```
79
81
80
-
Note that styles are not autoprefixed. To support older browsers, you need to supply corresponding style properties:
82
+
Nhớ rắng styles không có tự động tương thích với các trình duyệt nên bạn phải tự thêm tiếp đầu ngữ vào. Ví dụ:
81
83
82
84
```js
83
85
constdivStyle= {
84
-
WebkitTransition:'all', //note the capital 'W' here
85
-
msTransition:'all'// 'ms' is the only lowercase vendor prefix
86
+
WebkitTransition:'all', //nhớ là chữ 'W' được viết hoa
87
+
msTransition:'all'// 'ms' là tiếp đầu ngữ duy nhất được viết thường
86
88
};
87
89
88
90
functionComponentWithTransition() {
89
91
return<div style={divStyle}>This should work cross-browser</div>;
90
92
}
91
93
```
92
94
93
-
Style keys are camelCased in order to be consistent with accessing the properties on DOM nodes from JS (e.g. `node.style.backgroundImage`). Vendor prefixes [other than `ms`](https://www.andismith.com/blogs/2012/02/modernizr-prefixed/)should begin with a capital letter. This is why `WebkitTransition` has an uppercase "W".
95
+
Các thuộc tính css được camelCase để đồng nhất với Javascript, vd `node.style.backgroundImage`. Các tiếp đầu ngữ [ngoài `ms`](https://www.andismith.com/blogs/2012/02/modernizr-prefixed/)phải được bắt đầu bằng một chữ hoa vd như `WebkitTransition`.
94
96
95
-
React will automatically append a "px" suffix to certain numeric inline style properties. If you want to use units other than "px", specify the value as a string with the desired unit. For example:
97
+
React sẽ tự động thêm "px" vào sau các giá trị số. Nếu bạn muốn sử dụng đơn vị khác, hãy dùng kiểu string thay vì number, vd:
96
98
97
99
```js
98
100
// Result style: '10px'
@@ -106,37 +108,39 @@ React will automatically append a "px" suffix to certain numeric inline style pr
106
108
</div>
107
109
```
108
110
109
-
Not all style properties are converted to pixel strings though. Certain ones remain unitless (eg `zoom`, `order`, `flex`). A complete list of unitless properties can be seen [here](https://github.com/facebook/react/blob/4131af3e4bf52f3a003537ec95a1655147c81270/src/renderers/dom/shared/CSSProperty.js#L15-L59).
111
+
Không phải thuộc tính nào cũng được thêm "px" vào sau. Các thuộc tính không có đơn vị sẽ được giữ nguyên, vd như `zoom`, `order`, `flex`. Tất cả các thuộc tính không có đơn vị có thể được tìm thấy [ở đây](https://github.com/facebook/react/blob/4131af3e4bf52f3a003537ec95a1655147c81270/src/renderers/dom/shared/CSSProperty.js#L15-L59).
Normally, there is a warning when an element with children is also marked as `contentEditable`, because it won't work. This attribute suppresses that warning. Don't use this unless you are building a library like [Draft.js](https://facebook.github.io/draft-js/) that manages `contentEditable` manually.
115
+
Một component có component con được đánh dấu là `contentEditable` sẽ không hoạt động và sẽ được cảnh báo. Thuộc tính này sẽ bỏ đi cảnh báo, nhưng các bạn không cần thiết phải dùng nó trừ khi bạn đang xây dựng một thư viện làm việc trực tiếp với `contentEditable` như [Draft.js](https://facebook.github.io/draft-js/).
If you use server-side React rendering, normally there is a warning when the server and the client render different content. However, in some rare cases, it is very hard or impossible to guarantee an exact match. For example, timestamps are expected to differ on the server and on the client.
119
+
Nếu bạn sử dụng server-side rendering, thông thường sẽ có một cảnh báo khi nội dung được render trên server khác với client. Tuy nhiên, trong một vài trường hợp, rất khó để đảm báo server và client trùng khớp với nhau vd như render timestamp.
118
120
119
-
If you set`suppressHydrationWarning`to`true`, React will not warn you about mismatches in the attributes and the content of that element. It only works one level deep, and is intended to be used as an escape hatch. Don't overuse it. You can read more about hydration in the [`ReactDOM.hydrate()` documentation](/docs/react-dom.html#hydrate).
121
+
Bằng cách đặt`suppressHydrationWarning`là`true`, React sẽ không cảnh báo khi nội dung của client và server không khớp nhau. Tuy nhiên `suppressHydrationWarning` chỉ được áp dụng cho một lớp component và chỉ nên sử dụng trong trường hợp bất khả kháng, không nên lạm dụng nó. Xem thêm về Hydration tại [`ReactDOM.hydrate()`](/docs/react-dom.html#hydrate).
120
122
121
123
### value {#value}
122
124
123
-
The `value`attribute is supported by `<input>`and`<textarea>` components. You can use it to set the value of the component. This is useful for building controlled components. `defaultValue`is the uncontrolled equivalent, which sets the value of the component when it is first mounted.
125
+
Thuộc tính `value`được dùng cho `<input>`và`<textarea>`. Bạn có thể dùng `value` để tạo nên Controlled components. Dùng `defaultValue`nếu muốn Uncontrolled components.
124
126
125
-
## All Supported HTML Attributes {#all-supported-html-attributes}
127
+
## Tất cả thuộc tính HTML được hỗ trợ {#all-supported-html-attributes}
126
128
127
129
As of React 16, any standard [or custom](/blog/2017/09/08/dom-attributes-in-react-16.html) DOM attributes are fully supported.
128
130
129
-
React has always provided a JavaScript-centric API to the DOM. Since React components often take both custom and DOM-related props, React uses the `camelCase` convention just like the DOM APIs:
131
+
Trong React 16, [tất cả](/blog/2017/09/08/dom-attributes-in-react-16.html) thuộc tính DOM đều được hỗ trợ.
132
+
133
+
React đã luôn được viết với Javascript làm trọng tâm nên hầu hết các thuộc tính DOM đều được viết ở dạng `camelCase` tương tự như DOM API. Ví dụ như:
130
134
131
135
```js
132
-
<div tabIndex="-1"/>//Just like node.tabIndex DOM API
133
-
<div className="Button"/>//Just like node.className DOM API
134
-
<input readOnly={true} />//Just like node.readOnly DOM API
136
+
<div tabIndex="-1"/>//tương tự node.tabIndex DOM API
137
+
<div className="Button"/>//tương tự node.className DOM API
138
+
<input readOnly={true} />//tương tự node.readOnly DOM API
135
139
```
136
140
137
-
These props work similarly to the corresponding HTML attributes, with the exception of the special cases documented above.
141
+
Những thuộc tính này gần giống với thuộc tính HTML, với những ngoại lệ liệt kê ở trên.
138
142
139
-
Some of the DOM attributes supported by React include:
143
+
Một vài thuộc tính DOM dc React hỗ trợ như:
140
144
141
145
```
142
146
accept acceptCharset accessKey action allowFullScreen alt async autoComplete
0 commit comments