Skip to content

Commit 0e5309d

Browse files
feat: translate docs Typechecking With Proptypes page
1 parent 05db377 commit 0e5309d

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

content/docs/typechecking-with-proptypes.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
---
22
id: typechecking-with-proptypes
3-
title: Typechecking With PropTypes
3+
title: Kiểm Tra Type Với PropTypes
44
permalink: docs/typechecking-with-proptypes.html
55
redirect_from:
66
- "docs/react-api.html#typechecking-with-proptypes"
77
---
88

9-
> Note:
9+
> Ghi chú:
1010
>
11-
> `React.PropTypes` has moved into a different package since React v15.5. Please use [the `prop-types` library instead](https://www.npmjs.com/package/prop-types).
11+
> `React.PropTypes` đã được chuyển qua một package khác kể từ React v15.5. Hãy sử dụng [thư viện `prop-types` để thay thế](https://www.npmjs.com/package/prop-types).
1212
>
13-
>We provide [a codemod script](/blog/2017/04/07/react-v15.5.0.html#migrating-from-reactproptypes) to automate the conversion.
13+
>Chúng tôi cung cấp [tập lệnh codemod](/blog/2017/04/07/react-v15.5.0.html#migrating-from-reactproptypes) để thực hiện việc tự động chuyển đổi.
1414
15-
As your app grows, you can catch a lot of bugs with typechecking. For some applications, you can use JavaScript extensions like [Flow](https://flow.org/) or [TypeScript](https://www.typescriptlang.org/) to typecheck your whole application. But even if you don't use those, React has some built-in typechecking abilities. To run typechecking on the props for a component, you can assign the special `propTypes` property:
15+
Khi ứng dụng của bạn phát phiển, bạn sẽ có thể bắt gặp rất nhiều bug với việc kiểm tra type (kiểu). Đối với một với ứng dụng, bạn có thể sử dụng các JavaScript extension như [Flow](https://flow.org/) hoặc [TypeScript](https://www.typescriptlang.org/) để kiểm tra type cho toàn bộ ứng dụng của bạn. Nhưng ngay cả khi bạn không sử dụng chúng, React vẫn có xây dựng sẵn một vài kiểu kiểm tra type. Để chạy kiểm tra type trên prop cho một component, bạn có thể assign một property là `propTypes`:
1616

1717
```javascript
1818
import PropTypes from 'prop-types';
@@ -30,13 +30,13 @@ Greeting.propTypes = {
3030
};
3131
```
3232

33-
In this example, we are using a class component, but the same functionality could also be applied to function components, or components created by [`React.memo`](/docs/react-api.html#reactmemo) or [`React.forwardRef`](/docs/react-api.html#reactforwardref).
33+
Trong ví dụ này, chúng ta đang sử dụng một class component, nhưng nó cũng có thể áp tương tự với function component, hoặc component được tạo bởi [`React.memo`](/docs/react-api.html#reactmemo) hay [`React.forwardRef`](/docs/react-api.html#reactforwardref).
3434

35-
`PropTypes` exports a range of validators that can be used to make sure the data you receive is valid. In this example, we're using `PropTypes.string`. When an invalid value is provided for a prop, a warning will be shown in the JavaScript console. For performance reasons, `propTypes` is only checked in development mode.
35+
`PropTypes` sẽ export một loạt các validator nhằm đảm bảo rằng data (dữ liệu) được nhận vào là valid (hợp lệ). Trong ví dụ này, chúng ta sử dụng `PropTypes.string`. Khi một giá trị invalid (không hợp lệ) được cung cấp cho một prop, thì sẽ có một warning (cảnh báo) sẽ hiện lên bên trong JavaScript console. Vì lý do nhằm đảm bảo performance (hiệu suất), thì `propTypes` sẽ chỉ được kiểm tra trong quá môi trường development mode
3636

3737
### PropTypes {#proptypes}
3838

39-
Here is an example documenting the different validators provided:
39+
Đây là một ví dụ về các validator (trình xác thực) khác mà đã được React cung cấp:
4040

4141
```javascript
4242
import PropTypes from 'prop-types';
@@ -132,7 +132,7 @@ MyComponent.propTypes = {
132132

133133
### Requiring Single Child {#requiring-single-child}
134134

135-
With `PropTypes.element` you can specify that only a single child can be passed to a component as children.
135+
Với `PropTypes.element` bạn có thể chỉ định rằng chỉ có duy nhất một child có thể chuyển đến một component children.
136136

137137
```javascript
138138
import PropTypes from 'prop-types';
@@ -156,7 +156,7 @@ MyComponent.propTypes = {
156156

157157
### Default Prop Values {#default-prop-values}
158158

159-
You can define default values for your `props` by assigning to the special `defaultProps` property:
159+
Bạn có thể định nghĩa giá trị mặc định cho `props` của bạn bằng cách gán chúng cho một property là `defaultProps`:
160160

161161
```javascript
162162
class Greeting extends React.Component {
@@ -179,7 +179,7 @@ ReactDOM.render(
179179
);
180180
```
181181

182-
If you are using a Babel transform like [plugin-proposal-class-properties](https://babeljs.io/docs/en/babel-plugin-proposal-class-properties/) (previously _plugin-transform-class-properties_), you can also declare `defaultProps` as static property within a React component class. This syntax has not yet been finalized though and will require a compilation step to work within a browser. For more information, see the [class fields proposal](https://github.com/tc39/proposal-class-fields).
182+
Nếu bạn đang sử dụng Babel transform như là [plugin-proposal-class-properties](https://babeljs.io/docs/en/babel-plugin-proposal-class-properties/) (previously _plugin-transform-class-properties_), bạn cũng có thể khai báo `defaultProps` dưới dạng static property trong một React component class. Mặc dù vậy, cú pháp này vẫn chưa được hoàn thiện và sẽ yêu cầu qua một bước biên dịch để có thể hoạt động trên trình duyệt. Để biết thêm thông tin, hãy xem [class fields proposal](https://github.com/tc39/proposal-class-fields).
183183

184184
```javascript
185185
class Greeting extends React.Component {
@@ -195,13 +195,13 @@ class Greeting extends React.Component {
195195
}
196196
```
197197

198-
The `defaultProps` will be used to ensure that `this.props.name` will have a value if it was not specified by the parent component. The `propTypes` typechecking happens after `defaultProps` are resolved, so typechecking will also apply to the `defaultProps`.
198+
Việc sử dụng `defaultProps` nhằm đảm bảo rằng `this.props.name` sẽ có một giá trị mặc định nếu nó không được chỉ định truyền vào bởi component cha. Việc kiểm tra type `propTypes` sẽ xảy ra sau khi `defaultProps` giải quyết xong, vậy nên việc kiểm tra type cũng sẽ áp dụng cho `defaultProps`.
199199

200200
### Function Components {#function-components}
201201

202-
If you are using function components in your regular development, you may want to make some small changes to allow PropTypes to be properly applied.
202+
Nếu bạn đang sử dụng các function component trong quá trình phát triển thông thường của mình, bạn có thể muốn thực hiện một số thay đổi nhỏ để cho phép PropTypes được áp dụng đúng cách.
203203

204-
Let's say you have a component like this:
204+
Giả sử bạn có một component như sau:
205205

206206
```javascript
207207
export default function HelloWorldComponent({ name }) {
@@ -211,7 +211,7 @@ export default function HelloWorldComponent({ name }) {
211211
}
212212
```
213213

214-
To add PropTypes, you may want to declare the component in a separate function before exporting, like this:
214+
Để thêm PropTypes, bạn có thể khai báo component trong một hàm riêng trước khi export, như sau:
215215

216216
```javascript
217217
function HelloWorldComponent({ name }) {
@@ -223,7 +223,7 @@ function HelloWorldComponent({ name }) {
223223
export default HelloWorldComponent
224224
```
225225

226-
Then, you can add PropTypes directly to the `HelloWorldComponent`:
226+
Sau đó, bạn có thể thêm PropTypes trực tiếp vào `HelloWorldComponent`:
227227

228228
```javascript
229229
import PropTypes from 'prop-types'

0 commit comments

Comments
 (0)