eslint-plugin-react: no-unused-prop-types sometimes fails to detect props in functions on stateless components
Here is a minimal example that triggers this:
const PagingBlock = function(props) {
return (
<span>
<a onClick={() => props.previousPage()}/>
<a onClick={() => props.nextPage()}/>
</span>
);
};
PagingBlock.propTypes = {
nextPage: React.PropTypes.func.isRequired,
previousPage: React.PropTypes.func.isRequired,
};
The error message is:
18:17 error 'previousPage' PropType is defined but prop is never used react/no-unused-prop-types
Notes:
- Converting this to a React.createClass with static propTypes fixes the issue
- Removing either
<a/>tag produces the correct lint error, the false error only occurs when both are present - The error is notified for the first tag (switching
nextPageandpreviousPageinside the render function will make the error text saynextPageis defined but not used
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 36
- Comments: 33 (11 by maintainers)
this bug also triggers when use
...props.The error message is the same.
I think we should count all properties as used in this case.
I might do something wrong but this shows onItemClick propType not to be used:
Yeah, for me usage of a prop only in
componentWillReceivePropsalso throws this linting error.I have a similar problem with
PropTypes.shape.And I pass
errorto the child component as a whole.<MyChildComponent error={this.props.error} />ESLint throws
@healqq looks legit. I’d recommend opening a new issue
'onLoad' PropType is defined but prop is never used (react/no-unused-prop-types)@iegik & @ahauser31 -
I ran into the same issue and found that if you destructure
nextPropsinstead of using dot notation, the linting error is not thrown.I also confirmed in this thread: https://github.com/yannickcr/eslint-plugin-react/issues/884#issuecomment-253903073
I have the same error too with eslint-plugin-react v^7.19.0
Still throws error when:
@ljharb I’m sorry, pal, my bad. I’ve just mixed up this issue with another, which looks almost the same.
Experiencing the same issue too. Giving me an error saying ‘PropType is defined but prop is never used’ when my props is being used within a function inside a functional component.
Using eslint-plugin-react v^7.14.3 still throw this error
@wellingtonamaralleitao see #1232
Also failing to detect props being used on react lifecycle functions like
Still getting this in the most recent version of
eslint-plugin-reactin exactly the same code situation as the original example.