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
Copy file name to clipboardExpand all lines: docs/docs/typechecking-with-proptypes.md
+34-28Lines changed: 34 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,8 @@ redirect_from:
12
12
As your app grows, you can catch a lot of bugs with typechecking. For some applications, you can use JavaScript extensions like [Flow](https://flowtype.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:
13
13
14
14
```javascript
15
+
importPropTypesfrom'prop-types';
16
+
15
17
classGreetingextendsReact.Component {
16
18
render() {
17
19
return (
@@ -21,68 +23,70 @@ class Greeting extends React.Component {
21
23
}
22
24
23
25
Greeting.propTypes= {
24
-
name:React.PropTypes.string
26
+
name:PropTypes.string
25
27
};
26
28
```
27
29
28
-
`React.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 `React.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.
30
+
`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.
29
31
30
-
### React.PropTypes
32
+
### PropTypes
31
33
32
34
Here is an example documenting the different validators provided:
33
35
34
36
```javascript
37
+
importPropTypesfrom'prop-types';
38
+
35
39
MyComponent.propTypes= {
36
40
// You can declare that a prop is a specific JS primitive. By default, these
37
41
// are all optional.
38
-
optionalArray:React.PropTypes.array,
39
-
optionalBool:React.PropTypes.bool,
40
-
optionalFunc:React.PropTypes.func,
41
-
optionalNumber:React.PropTypes.number,
42
-
optionalObject:React.PropTypes.object,
43
-
optionalString:React.PropTypes.string,
44
-
optionalSymbol:React.PropTypes.symbol,
42
+
optionalArray:PropTypes.array,
43
+
optionalBool:PropTypes.bool,
44
+
optionalFunc:PropTypes.func,
45
+
optionalNumber:PropTypes.number,
46
+
optionalObject:PropTypes.object,
47
+
optionalString:PropTypes.string,
48
+
optionalSymbol:PropTypes.symbol,
45
49
46
50
// Anything that can be rendered: numbers, strings, elements or an array
47
51
// (or fragment) containing these types.
48
-
optionalNode:React.PropTypes.node,
52
+
optionalNode:PropTypes.node,
49
53
50
54
// A React element.
51
-
optionalElement:React.PropTypes.element,
55
+
optionalElement:PropTypes.element,
52
56
53
57
// You can also declare that a prop is an instance of a class. This uses
0 commit comments