Skip to content

Commit 51ad714

Browse files
committed
Add default fn param test and improve docs
1 parent 3582d5f commit 51ad714

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

docs/rules/require-default-props.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
This rule aims to ensure that any non-required `PropType` declaration of a component has a corresponding `defaultProps` value.
44

5-
One advantage of `defaultProps` over regular 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`.
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.
67

78
To illustrate, consider the following example:
89

tests/lib/rules/require-default-props.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,21 @@ ruleTester.run('require-default-props', rule, {
10291029
line: 8,
10301030
column: 3
10311031
}]
1032+
},
1033+
{
1034+
code: [
1035+
'var Greetings = ({ foo = "foo" }) => {',
1036+
' return <div>Hello {this.props.name}</div>;',
1037+
'}',
1038+
'Greetings.propTypes = {',
1039+
' foo: React.PropTypes.string',
1040+
'};'
1041+
].join('\n'),
1042+
errors: [{
1043+
message: 'propType "foo" is not required, but has no corresponding defaultProp declaration.',
1044+
line: 5,
1045+
column: 3
1046+
}]
10321047
}
10331048
]
10341049
});

0 commit comments

Comments
 (0)