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
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
Copy file name to clipboardExpand all lines: docs/rules/require-default-props.md
+16-3Lines changed: 16 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,18 @@
1
1
# Enforce a defaultProps definition for every prop that is not a required prop (react/require-default-props)
2
2
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.
4
5
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.
7
16
8
17
To illustrate, consider the following example:
9
18
@@ -337,3 +346,7 @@ If you don't care about using `defaultsProps` for your component's props that ar
337
346
338
347
# Resources
339
348
-[Official React documentation on defaultProps](https://facebook.github.io/react/docs/typechecking-with-proptypes.html#default-prop-values)
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.
4
4
5
5
## Rule Details
6
6
@@ -17,16 +17,18 @@ var Component = createReactClass({
17
17
},
18
18
...
19
19
});
20
-
21
-
classComponentextendsReact.Component {
20
+
```
21
+
```jsx
22
+
type Props = {
23
+
z: number,
24
+
a: any,
25
+
b: string
26
+
}
27
+
classComponentextendsReact.Component<Props> {
22
28
...
23
29
}
24
-
Component.propTypes= {
25
-
z:PropTypes.number,
26
-
a:PropTypes.any,
27
-
b:PropTypes.string
28
-
};
29
-
30
+
```
31
+
```jsx
30
32
classComponentextendsReact.Component {
31
33
static propTypes = {
32
34
z:PropTypes.any,
@@ -50,16 +52,18 @@ var Component = createReactClass({
0 commit comments