Skip to content
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

react/no-unused-prop-types false positive on destructured props #1002

Closed
victorias opened this issue Dec 14, 2016 · 8 comments · Fixed by #2301
Closed

react/no-unused-prop-types false positive on destructured props #1002

victorias opened this issue Dec 14, 2016 · 8 comments · Fixed by #2301

Comments

@victorias
Copy link

victorias commented Dec 14, 2016

react/no-unused-prop-types thinks that both the onClick and children props are unused even though these are clearly being used:

type Props = {
  onClick: () => void,
  children: any,
};

class Button extends Component {
  props: Props

  render () {
    const { props } = this;
    return (
      <button
        onClick={props.onClick}
      >
        {props.children}
      </button>
    );
  }
}

export default Button;

If I refer to onClick using this.props.onClick or destructure all the props from this.props it works.

@Augustin82
Copy link

Augustin82 commented Dec 15, 2016

We have been encountering the same bug (on an SFC).

@linuxonrails
Copy link

linuxonrails commented Jul 13, 2017

Same problem :-(

import React from 'react';

type User = {
  country: string;
};

export default ({ user }: { user: User }) => (
  <div>
    { user.country }
  </div>
);

'user.country' PropType is defined but prop is never used (react/no-unused-prop-types)

@DianaSuvorova
Copy link
Contributor

flow as well. Need a label. @ljharb I want to take a look into supporting flow soon. Gathering use cases. Thank you!

@DianaSuvorova
Copy link
Contributor

first use case here is a known issue with the rule when using an intermediate variable to store props/parts of props.

Second is fixed on master.

@derwaldgeist
Copy link

Same if you use a prop by simple deconstructing like this:

render({userId = 123}) {
  ...
}

@ljharb
Copy link
Member

ljharb commented Sep 5, 2017

@derwaldgeist you can't use props like that; render takes no arguments.

@derwaldgeist
Copy link

Yep, just found this out myself 👍 I was turning a stateless component in a class and missed this.

@jseminck
Copy link
Contributor

jseminck commented Sep 5, 2017

I've adding support for this in #1393 which is still WIP. Will add these test cases but I'm quite sure they work already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

7 participants