@@ -75,16 +75,17 @@ MyComponent.propTypes = {
75
75
// 可以指定一个数组由某一类型的元素组成
76
76
optionalArrayOf: PropTypes .arrayOf (PropTypes .number ),
77
77
78
- // 一个指定类型属性构成的对象
78
+ // 可以指定一个对象由某一类型的值组成
79
79
optionalObjectOf: PropTypes .objectOf (PropTypes .number ),
80
80
81
- // 一个指定属性及其类型的对象
81
+ // 可以指定一个对象由特定的类型值组成
82
82
optionalObjectWithShape: PropTypes .shape ({
83
83
color: PropTypes .string ,
84
84
fontSize: PropTypes .number
85
85
}),
86
86
87
- // 你可以在任何 PropTypes 属性后面加上 `isRequired` ,确保这个属性父组件没有提供时,会打印警告信息。
87
+ // 你可以在任何 PropTypes 属性后面加上 `isRequired` ,确保
88
+ // 这个 prop 没有被提供时,会打印警告信息。
88
89
requiredFunc: PropTypes .func .isRequired ,
89
90
90
91
// 任意类型的数据
@@ -104,7 +105,8 @@ MyComponent.propTypes = {
104
105
// 你也可以提供一个自定义的 `arrayOf` 或 `objectOf` 验证器。
105
106
// 它应该在验证失败时返回一个 Error 对象。
106
107
// 验证器将验证数组或对象中的每个值。验证器的前两个参数
107
- // 第一个是数组或对象本身,第二个是他们的键。
108
+ // 第一个是数组或对象本身
109
+ // 第二个是他们当前的键。
108
110
customArrayProp: PropTypes .arrayOf (function (propValue , key , componentName , location , propFullName ) {
109
111
if (! / matchme/ .test (propValue[key])) {
110
112
return new Error (
@@ -116,10 +118,9 @@ MyComponent.propTypes = {
116
118
};
117
119
```
118
120
119
- ### 限制一个元素 {#requiring-single-child}
121
+ ### 限制单个元素 {#requiring-single-child}
120
122
121
- 使用 ` PropTypes.element `
122
- 你可以指定 children 只有一个元素作为传递到组件。
123
+ 你可以通过 ` PropTypes.element ` 来确保传递给组件的 children 中只包含一个元素。
123
124
124
125
``` javascript
125
126
import PropTypes from ' prop-types' ;
@@ -141,7 +142,7 @@ MyComponent.propTypes = {
141
142
};
142
143
```
143
144
144
- ### 属性默认值 {#default-prop-values}
145
+ ### 默认 Prop 值 {#default-prop-values}
145
146
146
147
您可以通过配置特定的 ` defaultProps ` 属性来定义 ` props ` 的默认值:
147
148
@@ -166,7 +167,7 @@ ReactDOM.render(
166
167
);
167
168
```
168
169
169
- 如果你正在使用像 [ transform-class-properties] ( https://babeljs.io/docs/plugins/transform-class-properties/ ) 的 Babel 转换工具,你也可以在 React 组件类中声明 ` defaultProps ` 作为静态属性。此语法提案还没有最终确定,在浏览器中需要进行编译才能使用 。要了解更多信息,请查阅 [ class fields proposal] ( https://github.com/tc39/proposal-class-fields ) 。
170
+ 如果你正在使用像 [ transform-class-properties] ( https://babeljs.io/docs/plugins/transform-class-properties/ ) 的 Babel 转换工具,你也可以在 React 组件类中声明 ` defaultProps ` 作为静态属性。此语法提案还没有最终确定,需要进行编译后才能在浏览器中运行 。要了解更多信息,请查阅 [ class fields proposal] ( https://github.com/tc39/proposal-class-fields ) 。
170
171
171
172
``` javascript
172
173
class Greeting extends React .Component {
@@ -182,4 +183,4 @@ class Greeting extends React.Component {
182
183
}
183
184
```
184
185
185
- ` defaultProps ` 用于确保 ` this.props.name ` 在父组件没有指定值时 ,有一个默认值。` propTypes ` 类型检查发生在 ` defaultProps ` 赋值后,所以类型检查也适用于 ` defaultProps ` 。
186
+ ` defaultProps ` 用于确保 ` this.props.name ` 在父组件没有指定其值时 ,有一个默认值。` propTypes ` 类型检查发生在 ` defaultProps ` 赋值后,所以类型检查也适用于 ` defaultProps ` 。
0 commit comments