Skip to content

Commit d5aec61

Browse files
committed
📝 docs/typechecking-with-proptypes.md
1 parent 9c4e906 commit d5aec61

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed
+37-41
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: 使用 PropTypes 进行类型检查
44
permalink: docs/typechecking-with-proptypes.html
55
redirect_from:
66
- "docs/react-api.html#typechecking-with-proptypes"
77
---
88

9-
> Note:
9+
> 注意:
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 15.5 起,`React.PropTypes` 已移入另一个包中。请使用 [`prop-types`](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+
>我们提供了一个 [codemod](/blog/2017/04/07/react-v15.5.0.html#migrating-from-reactproptypes) 脚本来自动转换。
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+
随着你的应用程序不断增长,你可以通过类型检查捕获大量错误。对于某些应用程序来说,你可以使用 [Flow](https://flow.org/) [TypeScript](https://www.typescriptlang.org/) 等 JavaScript 扩展来对整个应用程序做类型检查。但即使你不使用这些扩展,React 也内置了一些类型检查的功能。要在组件的 props 上进行类型检查,你只需配置特定的 `propTypes` 属性:
1616

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

33-
`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.
33+
`PropTypes` 提供一系列验证器,可用于组件接收的数据类型是有效的。在本例中, 我们使用了 `PropTypes.string`。当传入类型不正确的属性时,JavaScript 控制台将会显示警告。出于性能方面的考虑,`propTypes` 只仅在开发模式下进行检查。
3434

35-
### PropTypes {#proptypes}
35+
### Prop 类型 {#proptypes}
3636

37-
Here is an example documenting the different validators provided:
37+
以下提供了使用不同验证器的例子:
3838

3939
```javascript
4040
import PropTypes from 'prop-types';
4141

4242
MyComponent.propTypes = {
43-
// You can declare that a prop is a specific JS type. By default, these
44-
// are all optional.
43+
// 你可以将属性声明为 JS 原生类型,默认情况下这些属性都是可选的。
4544
optionalArray: PropTypes.array,
4645
optionalBool: PropTypes.bool,
4746
optionalFunc: PropTypes.func,
@@ -50,50 +49,47 @@ MyComponent.propTypes = {
5049
optionalString: PropTypes.string,
5150
optionalSymbol: PropTypes.symbol,
5251

53-
// Anything that can be rendered: numbers, strings, elements or an array
54-
// (or fragment) containing these types.
52+
// 任何可被渲染的元素(包括数字、字符串、子元素或数组)
53+
//(或 Fragment)也包含这些类型。
5554
optionalNode: PropTypes.node,
5655

57-
// A React element.
56+
// 一个 React 元素
5857
optionalElement: PropTypes.element,
5958

60-
// You can also declare that a prop is an instance of a class. This uses
61-
// JS's instanceof operator.
59+
// 你也可以声明属性为某个类的实例,这里使用 JS 的 instanceof 操作符。
6260
optionalMessage: PropTypes.instanceOf(Message),
6361

64-
// You can ensure that your prop is limited to specific values by treating
65-
// it as an enum.
62+
// 你可以使你的属性只接收指定的值。
63+
// 这是一个枚举类型。
6664
optionalEnum: PropTypes.oneOf(['News', 'Photos']),
6765

68-
// An object that could be one of many types
66+
// 指定的多个对象类型中的一个
6967
optionalUnion: PropTypes.oneOfType([
7068
PropTypes.string,
7169
PropTypes.number,
7270
PropTypes.instanceOf(Message)
7371
]),
7472

75-
// An array of a certain type
73+
// 一个指定类型组成的数组
7674
optionalArrayOf: PropTypes.arrayOf(PropTypes.number),
7775

78-
// An object with property values of a certain type
76+
// 一个指定类型属性构成的对象
7977
optionalObjectOf: PropTypes.objectOf(PropTypes.number),
8078

81-
// An object taking on a particular shape
79+
// 一个指定属性及其类型的对象
8280
optionalObjectWithShape: PropTypes.shape({
8381
color: PropTypes.string,
8482
fontSize: PropTypes.number
8583
}),
8684

87-
// You can chain any of the above with `isRequired` to make sure a warning
88-
// is shown if the prop isn't provided.
85+
// 你可以在任何 PropTypes 属性后面加上 `isRequired` ,确保这个属性父组件没有提供时,会打印警告信息。
8986
requiredFunc: PropTypes.func.isRequired,
9087

91-
// A value of any data type
88+
// 任意类型的数据
9289
requiredAny: PropTypes.any.isRequired,
9390

94-
// You can also specify a custom validator. It should return an Error
95-
// object if the validation fails. Don't `console.warn` or throw, as this
96-
// won't work inside `oneOfType`.
91+
// 你可以指定一个自定义验证器。如果验证失败应该返回一个 Error 对象
92+
// 而不是 `console.warn` 或抛异常,因为在 `onOfType` 中不会起作用。
9793
customProp: function(props, propName, componentName) {
9894
if (!/matchme/.test(props[propName])) {
9995
return new Error(
@@ -103,11 +99,10 @@ MyComponent.propTypes = {
10399
}
104100
},
105101

106-
// You can also supply a custom validator to `arrayOf` and `objectOf`.
107-
// It should return an Error object if the validation fails. The validator
108-
// will be called for each key in the array or object. The first two
109-
// arguments of the validator are the array or object itself, and the
110-
// current item's key.
102+
// 你也可以提供一个自定义的 `arrayOf` 或 `objectOf` 验证器。
103+
// 它应该在验证失败时返回一个 Error 对象。
104+
// 验证器将验证数组或对象中的每个值。验证器的前两个参数
105+
// 第一个是数组或对象本身,第二个是他们的键。
111106
customArrayProp: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
112107
if (!/matchme/.test(propValue[key])) {
113108
return new Error(
@@ -119,16 +114,17 @@ MyComponent.propTypes = {
119114
};
120115
```
121116

122-
### Requiring Single Child {#requiring-single-child}
117+
### 限制一个元素 {#requiring-single-child}
123118

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 只有一个元素作为传递到组件。
125121

126122
```javascript
127123
import PropTypes from 'prop-types';
128124

129125
class MyComponent extends React.Component {
130126
render() {
131-
// This must be exactly one element or it will warn.
127+
// 这必须只有一个元素,否则控制台会打印警告。
132128
const children = this.props.children;
133129
return (
134130
<div>
@@ -143,9 +139,9 @@ MyComponent.propTypes = {
143139
};
144140
```
145141

146-
### Default Prop Values {#default-prop-values}
142+
### 属性默认值 {#default-prop-values}
147143

148-
You can define default values for your `props` by assigning to the special `defaultProps` property:
144+
您可以通过配置特定的 `defaultProps` 属性来定义 `props` 的默认值:
149145

150146
```javascript
151147
class Greeting extends React.Component {
@@ -156,19 +152,19 @@ class Greeting extends React.Component {
156152
}
157153
}
158154

159-
// Specifies the default values for props:
155+
// 指定 props 的默认值:
160156
Greeting.defaultProps = {
161157
name: 'Stranger'
162158
};
163159

164-
// Renders "Hello, Stranger":
160+
// 渲染出 "Hello, Stranger"
165161
ReactDOM.render(
166162
<Greeting />,
167163
document.getElementById('example')
168164
);
169165
```
170166

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).
167+
如果你正在使用像 [transform-class-properties](https://babeljs.io/docs/plugins/transform-class-properties/) 的 Babel 转换工具,你也可以在 React 组件类中声明 `defaultProps` 作为静态属性。此语法提案还没有最终确定,在浏览器中需要进行编译才能使用。要了解更多信息,请查阅 [class fields proposal](https://github.com/tc39/proposal-class-fields)
172168

173169
```javascript
174170
class Greeting extends React.Component {
@@ -184,4 +180,4 @@ class Greeting extends React.Component {
184180
}
185181
```
186182

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`.
183+
`defaultProps` 用于确保 `this.props.name` 在父组件没有指定值时,有一个默认值。`propTypes` 类型检查发生在 `defaultProps` 赋值后,所以类型检查也适用于 `defaultProps`

0 commit comments

Comments
 (0)