1
1
# Prevent missing props validation in a React component definition (react/prop-types)
2
2
3
- PropTypes improve the reusability of your component by validating the received data.
3
+ Defining types for component props improves reusability of your components by
4
+ validating received data. It can warn other developers if they make a mistake while reusing the component with improper data type.
4
5
5
- It can warn other developers if they make a mistake while reusing the component with improper data type.
6
+ You can provide types either in runtime types using [ PropTypes] or statically
7
+ using [ TypeScript] or [ Flow] . This rule will validate your prop types regardless
8
+ of how you're defining them.
6
9
7
10
## Rule Details
8
11
9
12
The following patterns are considered warnings:
10
13
11
14
``` jsx
12
- var Hello = createReactClass ({
13
- render : function () {
14
- return < div> Hello {this .props .name }< / div> ;
15
- }
16
- });
15
+ function Hello ({ name }) {
16
+ return < div> Hello {name}< / div> ;
17
+ // 'name' is missing in props validation
18
+ }
17
19
18
20
var Hello = createReactClass ({
19
21
propTypes: {
20
22
firstname: PropTypes .string .isRequired
21
23
},
22
24
render : function () {
23
- return < div> Hello {this .props .firstname } {this .props .lastname }< / div> ; // lastname type is not defined in propTypes
25
+ return < div> Hello {this .props .firstname } {this .props .lastname }< / div> ;
26
+ // 'lastname' type is missing in props validation
24
27
}
25
28
});
26
29
27
- function Hello ({ name }) {
30
+ // Or in ES6
31
+ class Hello extends React .Component {
32
+ render () {
33
+ return < div> Hello {this .props .firstname } {this .props .lastname }< / div> ;
34
+ // 'lastname' type is missing in props validation
35
+ }
36
+ }
37
+ Hello .propTypes = {
38
+ firstname: PropTypes .string .isRequired
39
+ }
40
+ ```
41
+
42
+ In TypeScript:
43
+
44
+ ``` tsx
45
+ interface Props = {
46
+ age: number
47
+ }
48
+ function Hello({ name }: Props ) {
28
49
return <div >Hello { name } </div >;
50
+ // 'name' type is missing in props validation
29
51
}
30
52
```
31
53
32
54
Examples of correct usage without warnings:
33
55
34
56
``` jsx
57
+ function Hello ({ name }) {
58
+ return < div> Hello {name}< / div> ;
59
+ }
60
+ Hello .propTypes = {
61
+ name: PropTypes .string .isRequired
62
+ }
63
+
35
64
var Hello = createReactClass ({
36
65
propTypes: {
37
66
name: PropTypes .string .isRequired ,
@@ -62,38 +91,31 @@ class HelloEs6WithPublicClassField extends React.Component {
62
91
}
63
92
```
64
93
65
- The following patterns are ** not ** considered warnings :
94
+ In Flow :
66
95
67
- ``` jsx
68
- var Hello = createReactClass ({
69
- render : function () {
70
- return < div> Hello World< / div> ;
71
- }
72
- });
73
-
74
- var Hello = createReactClass ({
75
- propTypes: {
76
- name: PropTypes .string .isRequired
77
- },
78
- render : function () {
96
+ ``` tsx
97
+ type Props = {
98
+ name: string
99
+ }
100
+ class Hello extends React .Component <Props > {
101
+ render() {
79
102
return <div >Hello { this .props .name } </div >;
80
103
}
81
- });
104
+ }
105
+ ```
82
106
83
- // Referencing an external object disable the rule for the component
84
- var Hello = createReactClass ({
85
- propTypes: myPropTypes,
86
- render : function () {
87
- return < div> Hello {this .props .name }< / div> ;
88
- }
89
- });
107
+ The following patterns are ** not** considered warnings:
90
108
109
+ ``` jsx
110
+ function Hello () {
111
+ return < div> Hello World< / div> ;
112
+ }
113
+
114
+ // Referencing an external object disable the rule for the component
91
115
function Hello ({ name }) {
92
116
return < div> Hello {name}< / div> ;
93
117
}
94
- Hello .propTypes = {
95
- name: PropTypes .string .isRequired ,
96
- };
118
+ Hello .propTypes = myPropTypes;
97
119
```
98
120
99
121
## Rule Options
@@ -121,11 +143,11 @@ As it aptly noticed in
121
143
122
144
> Why should children be an exception?
123
145
> Most components don't need ` this.props.children ` , so that makes it extra important
124
- to document ` children ` in the propTypes .
146
+ to document ` children ` in the prop types .
125
147
126
- Generally, you should use ` PropTypes.node ` for ` children ` . It accepts
127
- anything that can be rendered: numbers, strings, elements or an array containing
128
- these types.
148
+ Generally, you should use ` PropTypes.node ` or static type ` React.Node ` for
149
+ ` children ` . It accepts anything that can be rendered: numbers, strings, elements
150
+ or an array containing these types.
129
151
130
152
Since 2.0.0 children is no longer ignored for props validation.
131
153
@@ -135,6 +157,10 @@ For this rule to work we need to detect React components, this could be very har
135
157
136
158
For now we should detect components created with:
137
159
160
+ * a function that return JSX or the result of a ` React.createElement ` call.
138
161
* ` createReactClass() `
139
162
* an ES6 class that inherit from ` React.Component ` or ` Component `
140
- * a stateless function that return JSX or the result of a ` React.createElement ` call.
163
+
164
+ [ PropTypes ] : https://reactjs.org/docs/typechecking-with-proptypes.html
165
+ [ TypeScript ] : http://www.typescriptlang.org/
166
+ [ Flow ] : https://flow.org/
0 commit comments