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
> `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).
12
12
>
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.
14
14
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`:
16
16
17
17
```javascript
18
18
importPropTypesfrom'prop-types';
@@ -30,13 +30,13 @@ Greeting.propTypes = {
30
30
};
31
31
```
32
32
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).
34
34
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
36
36
37
37
### PropTypes {#proptypes}
38
38
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:
40
40
41
41
```javascript
42
42
importPropTypesfrom'prop-types';
@@ -132,7 +132,7 @@ MyComponent.propTypes = {
132
132
133
133
### Requiring Single Child {#requiring-single-child}
134
134
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 là children.
136
136
137
137
```javascript
138
138
importPropTypesfrom'prop-types';
@@ -156,7 +156,7 @@ MyComponent.propTypes = {
156
156
157
157
### Default Prop Values {#default-prop-values}
158
158
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`:
160
160
161
161
```javascript
162
162
classGreetingextendsReact.Component {
@@ -179,7 +179,7 @@ ReactDOM.render(
179
179
);
180
180
```
181
181
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).
183
183
184
184
```javascript
185
185
classGreetingextendsReact.Component {
@@ -195,13 +195,13 @@ class Greeting extends React.Component {
195
195
}
196
196
```
197
197
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`.
199
199
200
200
### Function Components {#function-components}
201
201
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.
203
203
204
-
Let's say you have a component like this:
204
+
Giả sử bạn có một component như sau:
205
205
206
206
```javascript
207
207
exportdefaultfunctionHelloWorldComponent({ name }) {
@@ -211,7 +211,7 @@ export default function HelloWorldComponent({ name }) {
211
211
}
212
212
```
213
213
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:
215
215
216
216
```javascript
217
217
functionHelloWorldComponent({ name }) {
@@ -223,7 +223,7 @@ function HelloWorldComponent({ name }) {
223
223
exportdefaultHelloWorldComponent
224
224
```
225
225
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`:
0 commit comments