-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Destructured use of property is not recognized by no-unused-prop-types #816
Comments
I am facing same issue with very similar use-case const propTypes = {
clientVersion: PropTypes.string,
};
const defaultProps = {
clientVersion: null,
};
class App extends Component {
constructor(props) {
super(props);
this.checkClientVersion = this.checkClientVersion.bind(this);
}
checkClientVersion(props = this.props) { // <-- might puzzle parser but still a valid use-case
const { clientVersion } = props; // <-- used!
<...>
}
render() {
<...>
}
App.propTypes = propTypes;
App.defaultProps = defaultProps;
export default App; |
@yury-dymov in your case, it's only used if they don't pass in a |
@ljharb, well, to me it looks very common componentDidMount() {
this.checkClientVersion();
}
componentWillReceiveProps(nextProps) {
<...>
this.checkClientVersion(nextProps);
}
checkClientVersion(props = this.props) {
const { clientVersion } = props;
<...>
} So, basically it is helper method, which is called to do whatever, when props are provided/updated. Normally it is called with no parameters but for |
If it's a helper method, it shouldn't be an instance method - and then this plugin won't warn on it. Instance methods should use |
Could this be related: #611 ? |
I've been thinking about the best way to handle this. The only thing I can think of is to build some common conventions into the rule. ie. Any function that uses the following patterns It will prevent false positive warnings, but would also not catch certain cases where the object isn't actually the component props. Thoughts? |
This is not linting rule, it's static analysis rule. Static analyzer should be able to resolve variables instead just checking them by some "mask". // user code
const { settings: { foo } } = this.props;
// plugin
resolve('foo') -> this.props.settings.foo // user code
const { users } = this.props;
users.map(({ id }) => ...);
// plugin
resolve('id') -> this.props.users[].id |
I got false positive connected to destruction assignment. Code preview export default class NavigationButton extends React.Component {
static propTypes = {
route: PropTypes.shape({
getBarTintColor: PropTypes.func.isRequired,
}).isRequired,
};
renderTitle() {
const { route } = this.props;
return <Title tintColor={route.getBarTintColor()}>TITLE</Title>;
}
} I get this false report, despite
|
I am seeing this as well when using |
Sorry to pile on, but I believe this is one more unique example of inaccurate triggering of the when props are destructured and reassigned.
Throws two errors: |
@numbergames const Component = ({ children: aNode }) => (
// ...
);
// is equivalent of this
const Component = (props) => {
const { children: aNode } = props;
// ...
} Thus, all prop validation does apply to original prop names - not aliased. Same applies to Correct usage would be: import React from 'react';
const Component = ({ children: aNode }) => (
<div>
{aNode}
</div>
);
Component.defaultProps = {
children: null,
};
Component.propTypes = {
children: React.PropTypes.node,
}; |
Here's an even weirder bug that may be related to all this. I'm using flow and this keeps coming up: onInputChange({ target: { value: inputValue } }: { target: { value: string } }) {
// ^^^^
// target.value PropType is defined but prop is never used (react/no-unused-prop-types)
this.setState({
inputValue,
inputWidth: Select.defaultInputWidth + (7 * inputValue.length),
});
} I have no idea why this would think there are props here. It's inside a class component; it's not even related to props, I'm just destructuring an event object. |
@mike-robertson yours is likely a bug specific to Flow; can you file a separate issue for that? |
My code in react component
In PropTypes
still the error reads
|
@anshulsahni Can you share more code? Where does that find appear? |
this is the full component code, certain pieces of code are hidden
and the error reads
|
I have a warning with flow too. My code:
And my solution:
I can create a repro if it's necessary. |
Just adding my snippet here... type HeaderPropTypes = {
actions: { unAuth: () => any }, // error 'actions.unAuth' PropType is defined but prop is never used react/no-unused-prop-types
};
const Header = ({ actions }: HeaderPropTypes): React$Element<*> => (
<div>
<button
className="btn--secondary"
onClick={
function logout(): boolean {
flushAuth(actions.unAuth);
return true;
}
}
>
Log Out
</button>
</div>
);
const mapDispatchToProps = (dispatch: () => any): {} => ({
actions: bindActionCreators(Actions, dispatch),
}); Following @mqklin my solution was to disable that specific rule... type HeaderPropTypes = {
/* eslint-disable react/no-unused-prop-types */
actions: { unAuth: () => any },
/* eslint-enable react/no-unused-prop-types */
}; |
@ljharb any update on this ? |
@ljharb Ok I had no idea there were more than syntax differences.. thanks so much! |
@ljharb I have created a PR with (what I think) all of the valid use cases discussed in this thread. |
@ljharb, here is the summary of this thread. There are 3 separate types of errors were reported/discussed here:
With that I think we can actually close this ticket. I will update my pr to format the tests correctly. But there is going to be no code change. LMK your thoughts. --Update--- |
Thanks for that summary - I'd say the flow issues should be supported, ideally, by a separate PR is fine; and the issues with shape props are known. |
The flow case is fixed? Exists other issue for it? |
I believe there's been a regression in 7.14.3. When I destructure a proptype while in 7.14.3, it errors out for me. When I pin the version to 7.14.2, no errors. |
@djbobbydrake what about v7.15.1? |
@ljharb errors out for me in 7.15.1 also |
Thanks for confirming; can you please file a new issue? The sooner we can get a test case the sooner we can fix it :-) |
Given a React component like:
And the following eslint rule:
I see the following error:
This is incorrect because the
i18n
property is destructured into a new constant and then thegettext()
function is called. If I edit the code so that it doesn't use destructuring then the error goes away, revealing the bug.Here is a small app that reproduces it: eslint-scratch.zip. Run:
This is similar to #782 but they seem different.
The text was updated successfully, but these errors were encountered: