Skip to content

no-unused-prop-types rule does not recognise component that returns an array #1682

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

Open
leonaves opened this issue Feb 8, 2018 · 6 comments

Comments

@leonaves
Copy link

leonaves commented Feb 8, 2018

Example:

import React from 'react';
import PropTypes from 'prop-types';

const TestComponent = () => (
  ['one', 'two', 'three'].map(text => <div>{ text }</div>)
);

TestComponent.propTypes = {
  customClass: PropTypes.string,
};

export default TestComponent;

React 16 allows for arrays to be returned from components, but in the above example, the no-unused-prop-types seems to not recognise that customClass is unused. It does detect it when the component is changed to:

const TestComponent = () => {
  const componentArr = ['one', 'two', 'three'].map(text => <div>{ text }</div>);
  return <div>{ componentArr }</div>;
};
@leonaves leonaves changed the title no-unused-prop-types Rule does not recognise component that returns an array no-unused-prop-types rule does not recognise component that returns an array Feb 8, 2018
@ljharb
Copy link
Member

ljharb commented Feb 9, 2018

Our component detection likely requires a component to return JSX.

It becomes very very difficult to detect if an SFC that returns no JSX is in fact a component. A temporary workaround should be to add a comment above the SFC that says /* @extends React.Component */.

@nodkrot
Copy link

nodkrot commented Feb 18, 2019

@ljharb
Copy link
Member

ljharb commented Feb 18, 2019

Note that you can also return a React.Fragment or <>.

@nodkrot
Copy link

nodkrot commented Feb 18, 2019

That could work as a wrapper 👍

@leonaves
Copy link
Author

leonaves commented Mar 2, 2019

Forgot about this, but re-reading, why do you need to detect that the function is a component? If the prop is unused in the function body then the rule should error, it shouldn't matter if it returns JSX or not.

@ljharb
Copy link
Member

ljharb commented Mar 3, 2019

@leonaves if it's not a component, then it doesn't have "props" - it has arguments, and it can't have propTypes.

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

No branches or pull requests

3 participants