|
| 1 | +# Prevent definitions of unused prop types (unused-prop-types) |
| 2 | + |
| 3 | +Warns you if you have defined a prop type but it is never being used anywhere. |
| 4 | + |
| 5 | +## Rule Details |
| 6 | + |
| 7 | +The following patterns are considered warnings: |
| 8 | + |
| 9 | +```jsx |
| 10 | +var Hello = React.createClass({ |
| 11 | + propTypes: { |
| 12 | + name: React.PropTypes.string |
| 13 | + }, |
| 14 | + render: function() { |
| 15 | + return <div>Hello Bob</div>; |
| 16 | + } |
| 17 | +}); |
| 18 | + |
| 19 | +var Hello = React.createClass({ |
| 20 | + propTypes: { |
| 21 | + firstname: React.PropTypes.string.isRequired, |
| 22 | + middlename: React.PropTypes.string.isRequired, // middlename is never used below |
| 23 | + lastname: React.PropTypes.string.isRequired |
| 24 | + }, |
| 25 | + render: function() { |
| 26 | + return <div>Hello {this.props.firstname} {this.props.lastname}</div>; |
| 27 | + } |
| 28 | +}); |
| 29 | + |
| 30 | +function Hello({ name }) { |
| 31 | + return <div>Hello Jeremy</div>; |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +The following patterns are not considered warnings: |
| 36 | + |
| 37 | +```jsx |
| 38 | +var Hello = React.createClass({ |
| 39 | + propTypes: { |
| 40 | + name: React.PropTypes.string |
| 41 | + }, |
| 42 | + render: function() { |
| 43 | + return <div>Hello {this.props.name}</div>; |
| 44 | + } |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +## Rule Options |
| 49 | + |
| 50 | +This rule can take one argument to ignore some specific props during validation. |
| 51 | + |
| 52 | +``` |
| 53 | +... |
| 54 | +"prop-types": [<enabled>, { customValidators: <customValidator> }] |
| 55 | +... |
| 56 | +``` |
| 57 | + |
| 58 | +* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0. |
| 59 | +* `customValidators`: optional array of validators used for propTypes validation. |
| 60 | +* `skipShapeProps`: In some cases it is impossible to accurately detect whether or not a shape PropType's values are being used. Setting this option to true will skip validation of shape PropTypes. |
| 61 | + |
| 62 | +## About component detection |
| 63 | + |
| 64 | +For this rule to work we need to detect React components, this could be very hard since components could be declared in a lot of ways. |
| 65 | + |
| 66 | +For now we should detect components created with: |
| 67 | + |
| 68 | +* `React.createClass()` |
| 69 | +* an ES6 class that inherit from `React.Component` or `Component` |
| 70 | +* a stateless function that return JSX or the result of a `React.createElement` call. |
0 commit comments