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).
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:
`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.
### Requiring Single Child {#requiring-single-child}
117
+
### 限制一个元素 {#requiring-single-child}
123
118
124
-
With `PropTypes.element` you can specify that only a single child can be passed to a component as children.
119
+
使用 `PropTypes.element`
120
+
你可以指定 children 只有一个元素作为传递到组件。
125
121
126
122
```javascript
127
123
importPropTypesfrom'prop-types';
128
124
129
125
classMyComponentextendsReact.Component {
130
126
render() {
131
-
//This must be exactly one element or it will warn.
127
+
//这必须只有一个元素,否则控制台会打印警告。
132
128
constchildren=this.props.children;
133
129
return (
134
130
<div>
@@ -143,9 +139,9 @@ MyComponent.propTypes = {
143
139
};
144
140
```
145
141
146
-
### Default Prop Values {#default-prop-values}
142
+
### 属性默认值 {#default-prop-values}
147
143
148
-
You can define default values for your `props` by assigning to the special `defaultProps` property:
144
+
您可以通过配置特定的 `defaultProps` 属性来定义 `props` 的默认值:
149
145
150
146
```javascript
151
147
classGreetingextendsReact.Component {
@@ -156,19 +152,19 @@ class Greeting extends React.Component {
156
152
}
157
153
}
158
154
159
-
//Specifies the default values for props:
155
+
//指定 props 的默认值:
160
156
Greeting.defaultProps= {
161
157
name:'Stranger'
162
158
};
163
159
164
-
//Renders "Hello, Stranger":
160
+
//渲染出 "Hello, Stranger":
165
161
ReactDOM.render(
166
162
<Greeting />,
167
163
document.getElementById('example')
168
164
);
169
165
```
170
166
171
-
If you are using a Babel transform like [transform-class-properties](https://babeljs.io/docs/plugins/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).
@@ -184,4 +180,4 @@ class Greeting extends React.Component {
184
180
}
185
181
```
186
182
187
-
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`.
0 commit comments