Skip to content

Commit 9a4baf6

Browse files
committed
[Docs] Clean up examples in rule 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. Also, document that rules like `prop-types` and `require-default-props` recognize static types, too
1 parent e69b113 commit 9a4baf6

File tree

6 files changed

+156
-76
lines changed

6 files changed

+156
-76
lines changed

docs/rules/boolean-prop-naming.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Allows you to enforce a consistent naming pattern for props which expect a boolean value.
44

5+
> **Note**: You can provide types in runtime types using [PropTypes] and/or
6+
statically using [TypeScript] or [Flow]. This rule will validate your prop types
7+
regardless of how you define them.
8+
59
## Rule Details
610

711
The following patterns are considered warnings:
@@ -15,6 +19,13 @@ var Hello = createReactClass({
1519
});
1620
```
1721

22+
```jsx
23+
type Props = {
24+
enabled: boolean
25+
}
26+
const Hello = (props: Props) => <div />;
27+
```
28+
1829
The following patterns are **not** considered warnings:
1930

2031
```jsx
@@ -25,16 +36,22 @@ var Hello = createReactClass({
2536
render: function() { return <div />; };
2637
});
2738
```
39+
```jsx
40+
type Props = {
41+
isEnabled: boolean
42+
}
43+
const Hello = (props: Props) => <div />
44+
```
2845

2946
## Rule Options
3047

3148
```js
3249
...
33-
"react/boolean-prop-naming": [<enabled>, {
34-
"propTypeNames": Array<string>,
35-
"rule": <string>,
36-
"message": <string>,
37-
"validateNested": <boolean>
50+
"react/boolean-prop-naming": [<enabled>, {
51+
"propTypeNames": Array<string>,
52+
"rule": <string>,
53+
"message": <string>,
54+
"validateNested": <boolean>
3855
}]
3956
...
4057
```
@@ -99,3 +116,7 @@ This value is boolean. It tells if nested props should be validated as well. By
99116
```jsx
100117
"react/boolean-prop-naming": ["error", { "validateNested": true }]
101118
```
119+
120+
[PropTypes]: https://reactjs.org/docs/typechecking-with-proptypes.html
121+
[TypeScript]: http://www.typescriptlang.org/
122+
[Flow]: https://flow.org/

docs/rules/default-props-match-prop-types.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# Enforce all defaultProps have a corresponding non-required PropType (react/default-props-match-prop-types)
22

3-
This rule aims to ensure that any `defaultProp` has a non-required `PropType` declaration.
3+
This rule aims to ensure that any prop in `defaultProps` has a non-required type
4+
definition.
45

5-
Having `defaultProps` for non-existent `propTypes` is likely the result of errors in refactoring
6-
or a sign of a missing `propType`. Having a `defaultProp` for a required property similarly
7-
indicates a possible refactoring problem.
6+
> **Note**: You can provide types in runtime types using [PropTypes] and/or
7+
statically using [TypeScript] or [Flow]. This rule will validate your prop types
8+
regardless of how you define them.
9+
10+
Having `defaultProps` for non-existent prop types is likely the result of errors
11+
in refactoring or a sign of a missing prop type. Having a `defaultProp` for a
12+
required property similarly indicates a possible refactoring problem.
813

914
## Rule Details
1015

@@ -160,7 +165,7 @@ NotAComponent.propTypes = {
160165

161166
### `allowRequiredDefaults`
162167

163-
When `true` the rule will ignore `defaultProps` for `isRequired` `propTypes`.
168+
When `true` the rule will ignore `defaultProps` for required prop types.
164169

165170
The following patterns are considered okay and do not cause warnings:
166171

@@ -190,3 +195,6 @@ If you don't care about stray `defaultsProps` in your components, you can disabl
190195
# Resources
191196
- [Official React documentation on defaultProps](https://facebook.github.io/react/docs/typechecking-with-proptypes.html#default-prop-values)
192197

198+
[PropTypes]: https://reactjs.org/docs/typechecking-with-proptypes.html
199+
[TypeScript]: http://www.typescriptlang.org/
200+
[Flow]: https://flow.org/

docs/rules/no-unused-prop-types.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Prevent definitions of unused propTypes (react/no-unused-prop-types)
22

3-
Warns if a propType isn't being used.
3+
Warns if a prop with a defined type isn't being used.
4+
5+
> **Note**: You can provide types in runtime types using [PropTypes] and/or
6+
statically using [TypeScript] or [Flow]. This rule will validate your prop types
7+
regardless of how you define them.
48

59
## Rule Details
610

@@ -19,17 +23,17 @@ Hello.propTypes = {
1923
```
2024

2125
```jsx
22-
class Hello extends React.Component {
26+
type Props = {
27+
firstname: string,
28+
middlename: string, // middlename is never used above
29+
lastname: string
30+
}
31+
32+
class Hello extends React.Component<Props> {
2333
render() {
2434
return <div>Hello {this.props.firstname} {this.props.lastname}</div>;
2535
}
2636
}
27-
28-
Hello.propTypes: {
29-
firstname: PropTypes.string.isRequired,
30-
middlename: PropTypes.string.isRequired, // middlename is never used above
31-
lastname: PropTypes.string.isRequired
32-
},
3337
```
3438

3539
The following patterns are **not** considered warnings:
@@ -114,3 +118,7 @@ AComponent.propTypes = {
114118
bProp: PropTypes.string
115119
};
116120
```
121+
122+
[PropTypes]: https://reactjs.org/docs/typechecking-with-proptypes.html
123+
[TypeScript]: http://www.typescriptlang.org/
124+
[Flow]: https://flow.org/

docs/rules/prop-types.md

Lines changed: 64 additions & 38 deletions
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+
> **Note**: You can provide types in runtime types using [PropTypes] and/or
7+
statically using [TypeScript] or [Flow]. This rule will validate your prop types
8+
regardless of how you define 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/

docs/rules/require-default-props.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
# Enforce a defaultProps definition for every prop that is not a required prop (react/require-default-props)
22

3-
This rule aims to ensure that any non-required `PropType` declaration of a component has a corresponding `defaultProps` value.
3+
This rule aims to ensure that any non-required prop types of a component has a
4+
corresponding `defaultProps` value.
45

5-
One advantage of `defaultProps` over custom default logic in your code is that `defaultProps` are resolved by React before the `PropTypes` typechecking happens, so typechecking will also apply to your `defaultProps`.
6-
The same also holds true for stateless functional components: default function parameters do not behave the same as `defaultProps` and thus using `defaultProps` is still preferred.
6+
> **Note**: You can provide types in runtime types using [PropTypes] and/or
7+
statically using [TypeScript] or [Flow]. This rule will validate your prop types
8+
regardless of how you define them.
9+
10+
One advantage of `defaultProps` over custom default logic in your code is that
11+
`defaultProps` are resolved by React before the `PropTypes` typechecking
12+
happens, so typechecking will also apply to your `defaultProps`. The same also
13+
holds true for stateless functional components: default function parameters do
14+
not behave the same as `defaultProps` and thus using `defaultProps` is still
15+
preferred.
716

817
To illustrate, consider the following example:
918

@@ -337,3 +346,7 @@ If you don't care about using `defaultsProps` for your component's props that ar
337346

338347
# Resources
339348
- [Official React documentation on defaultProps](https://facebook.github.io/react/docs/typechecking-with-proptypes.html#default-prop-values)
349+
350+
[PropTypes]: https://reactjs.org/docs/typechecking-with-proptypes.html
351+
[TypeScript]: http://www.typescriptlang.org/
352+
[Flow]: https://flow.org/

docs/rules/sort-prop-types.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Enforce propTypes declarations alphabetical sorting (react/sort-prop-types)
22

3-
Some developers prefer to sort propTypes declarations alphabetically to be able to find necessary declaration easier at the later time. Others feel that it adds complexity and becomes burden to maintain.
3+
Some developers prefer to sort prop type declaratioms alphabetically to be able to find necessary declaration easier at the later time. Others feel that it adds complexity and becomes burden to maintain.
44

55
## Rule Details
66

@@ -17,16 +17,18 @@ var Component = createReactClass({
1717
},
1818
...
1919
});
20-
21-
class Component extends React.Component {
20+
```
21+
```jsx
22+
type Props = {
23+
z: number,
24+
a: any,
25+
b: string
26+
}
27+
class Component extends React.Component<Props> {
2228
...
2329
}
24-
Component.propTypes = {
25-
z: PropTypes.number,
26-
a: PropTypes.any,
27-
b: PropTypes.string
28-
};
29-
30+
```
31+
```jsx
3032
class Component extends React.Component {
3133
static propTypes = {
3234
z: PropTypes.any,
@@ -50,16 +52,18 @@ var Component = createReactClass({
5052
},
5153
...
5254
});
53-
54-
class Component extends React.Component {
55+
```
56+
```jsx
57+
type Props = {
58+
a: string,
59+
b: any,
60+
c: string,
61+
}
62+
class Component extends React.Component<Props> {
5563
...
5664
}
57-
Component.propTypes = {
58-
a: PropTypes.string,
59-
b: PropTypes.any,
60-
c: PropTypes.string
61-
};
62-
65+
```
66+
```jsx
6367
class Component extends React.Component {
6468
static propTypes = {
6569
a: PropTypes.any,

0 commit comments

Comments
 (0)