Closed
Description
h2. Expected
When a propType React.PropTypes.arrayOf(<something>)
, you shouldn't be prompted to add
length
by eslint.
h2. What happens
You get '.length' is missing in props validation (react/prop-types) from eslint
h2. Example
class MyClass extends React.Component {
static propTypes = {
results: PropTypes.arrayOf(
PropTypes.shape({
items: PropTypes.arrayOf(PropTypes.shape(<Shape definition>))
})
)
};
get hasItems() {
this.props.results && this.props.results.items && this.props.results.items.length > 0;
}
}
Results in this:
'results.items.length' is missing in props validation (react/prop-types)
Activity
guidobouman commentedon Feb 1, 2017
@ljharb Any pointers as to where it would be smart to implement the filtering of default methods & properties? Somewhere in https://github.com/yannickcr/eslint-plugin-react/blob/master/lib/rules/prop-types.js ofcourse. But at what stage might this be best to implement?
reportUndeclaredPropTypes()
&Program:exit()
seem a little late in the chain.karissame commentedon Mar 24, 2017
I have a similar issue- is there any way to get the length of an array in your component's props?
ljharb commentedon Mar 24, 2017
@karissame I built
withShape
inairbnb-prop-types
specifically so you could dowithShape(PropTypes.array, { length: PropTypes.number })
or similar, since React.PropTypes makes that impossible.That said, the plugin should definitely exempt anything on Array.prototype when PropTypes.array or arrayOf is used.
[Tests]`prop-types`: add passing test case about array.length
prop-types
: add passing test case about array.length #2290Update eslint-plugin-react to the latest version 🚀 (#88)
prop-types
rule reports for Array.prototype.reduce is missing #238342 remaining items