Skip to content

Commit 3ef1208

Browse files
silvenonljharb
authored andcommitted
[Docs] prop-types: Clean up examples in prop-types docs
These examples use `createReactClass` a lot, which is legacy at this point. I'm modifying examples to primarily use function components because those are the ones people are using most often today. Other ways of defining a component are just here to show that this rule recognizes them, too. Document that prop-types accepts static types, too
1 parent e69b113 commit 3ef1208

File tree

1 file changed

+64
-38
lines changed

1 file changed

+64
-38
lines changed

docs/rules/prop-types.md

+64-38
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,66 @@
11
# Prevent missing props validation in a React component definition (react/prop-types)
22

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.
45

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.
69

710
## Rule Details
811

912
The following patterns are considered warnings:
1013

1114
```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+
}
1719

1820
var Hello = createReactClass({
1921
propTypes: {
2022
firstname: PropTypes.string.isRequired
2123
},
2224
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
2427
}
2528
});
2629

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) {
2849
return <div>Hello {name}</div>;
50+
// 'name' type is missing in props validation
2951
}
3052
```
3153

3254
Examples of correct usage without warnings:
3355

3456
```jsx
57+
function Hello({ name }) {
58+
return <div>Hello {name}</div>;
59+
}
60+
Hello.propTypes = {
61+
name: PropTypes.string.isRequired
62+
}
63+
3564
var Hello = createReactClass({
3665
propTypes: {
3766
name: PropTypes.string.isRequired,
@@ -62,38 +91,31 @@ class HelloEs6WithPublicClassField extends React.Component {
6291
}
6392
```
6493

65-
The following patterns are **not** considered warnings:
94+
In Flow:
6695

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() {
79102
return <div>Hello {this.props.name}</div>;
80103
}
81-
});
104+
}
105+
```
82106

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:
90108

109+
```jsx
110+
function Hello() {
111+
return <div>Hello World</div>;
112+
}
113+
114+
// Referencing an external object disable the rule for the component
91115
function Hello({ name }) {
92116
return <div>Hello {name}</div>;
93117
}
94-
Hello.propTypes = {
95-
name: PropTypes.string.isRequired,
96-
};
118+
Hello.propTypes = myPropTypes;
97119
```
98120

99121
## Rule Options
@@ -121,11 +143,11 @@ As it aptly noticed in
121143

122144
> Why should children be an exception?
123145
> 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.
125147

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.
129151

130152
Since 2.0.0 children is no longer ignored for props validation.
131153

@@ -135,6 +157,10 @@ For this rule to work we need to detect React components, this could be very har
135157

136158
For now we should detect components created with:
137159

160+
* a function that return JSX or the result of a `React.createElement` call.
138161
* `createReactClass()`
139162
* 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

Comments
 (0)