Skip to content

Commit ed72341

Browse files
authored
Merge pull request #1329 from DianaSuvorova/docs
known issues for no-used-prop-types
2 parents 3195805 + 8d97fb6 commit ed72341

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

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

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,54 @@ This rule can take one argument to ignore some specific props during validation.
5555
* `customValidators`: optional array of validators used for propTypes validation.
5656
* `skipShapeProps`: In some cases it is impossible to accurately detect whether or not a `PropTypes.shape`'s values are being used. Setting this option to `true` will skip validation of `PropTypes.shape` (`true` by default).
5757

58-
## Caveats
58+
## Known Issues/Limitations
5959

60-
This rule does not track component props as they move from function to function or during variable renaming (such as in the event of prop object destructuring assignments). As such, it's prone to false positives in situations where the prop use cannot be accurately detected.
60+
***False positives*** for components with Stateless Functional Components;
61+
SFC is a function that takes prop(s) as an argument and returns a JSX expression.
62+
Even if this function gets called from a component the props that are only used inside SFC would not be considered used by a component.
6163

62-
## About component detection
6364

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+
Triggers false positive:
66+
```js
67+
function AComponent(props) {
68+
function helperRenderer(aProp) { // is considered SFC
69+
return (
70+
<span>{aProp}{props.bProp}</span>
71+
);
72+
}
6573

66-
For now we should detect components created with:
74+
return (
75+
<div>
76+
{helperRenderer(props.aProp)}
77+
</div>
78+
);
79+
}
80+
81+
AComponent.propTypes = {
82+
aProp: PropTypes.string,
83+
bProp: PropTypes.string // bProp is defined but never used
84+
};
85+
```
86+
A suggested fix is to assign a bProp to a variable outside of the SFC.
6787

68-
* `createReactClass()`
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.
88+
```js
89+
function AComponent(props) {
90+
const { bProp } = props
91+
function helperRenderer(aProp) { // is considered SFC
92+
return (
93+
<span>{aProp}{bProp}</span>
94+
);
95+
}
96+
97+
return (
98+
<div>
99+
{helperRenderer(props.aProp)}
100+
</div>
101+
);
102+
}
103+
104+
AComponent.propTypes = {
105+
aProp: PropTypes.string,
106+
bProp: PropTypes.string
107+
};
108+
```

0 commit comments

Comments
 (0)