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

7.12.0 can't add properties to propType #2122

Closed
cyrildurand opened this issue Jan 5, 2019 · 5 comments
Closed

7.12.0 can't add properties to propType #2122

cyrildurand opened this issue Jan 5, 2019 · 5 comments

Comments

@cyrildurand
Copy link

cyrildurand commented Jan 5, 2019

Since v7.12.0 it is not possible to alter propTypes anymore

Let's consider this code

        const personPropTypes = {};
        personPropTypes.name = PropTypes.string.isRequired;
        var Hello = createReactClass({
          propTypes: personPropTypes,,
          render: function() {,
            return <div>Hello {this.props.name}</div>;,
          },
        });

With 7.11.1 this code validates without error but since 7.12.0 we have a

name is missing in props validation

I need to alter the propTypes object to support hierarchy. ie :

    const menuItemPropTypes = {
      id: PropTypes.string.isRequired,
      title: PropTypes.string.isRequired,
      path: PropTypes.string,
    };
    menuItemPropTypes.menuItems = PropTypes.arrayOf(PropTypes.shape(menuItemPropTypes));
@ljharb
Copy link
Member

ljharb commented Jan 5, 2019

That’s not actually necessary - try this:

    let menuItemPropTypes;
    menuItemPropTypes = {
      id: PropTypes.string.isRequired,
      title: PropTypes.string.isRequired,
      path: PropTypes.string,
      menuItems(...args) {
        return PropTypes.arrayOf(PropTypes.shape(menuItemPropTypes))(...args);
      },
    };

Mutation after the fact is almost never required.

@cyrildurand

This comment has been minimized.

@ljharb

This comment has been minimized.

@yannickcr
Copy link
Member

Are you sure this is a regression with v7.12.0 ? I tried with v7.11.1 and I still have the name is missing in props validation error.

@cyrildurand
Copy link
Author

After further investigation it looks like I can reproduce the issue with this minimal code

const fooPropTypes = {
  firstName: PropTypes.string.isRequired,
};
fooPropTypes.lastName = PropTypes.string.isRequired;

export class FooComponent extends React.Component {
  static propTypes = fooPropTypes;

  render() {
    const { firstName, lastName, invalid } = this.props;
    const o = { firstName, lastName, invalid };
    return <span {...o} />;
  }
}

With 7.11.0 the spread operator ({...o}) acts like it disabled eslint for my class : I have no error for lastName nor invalid. If I remove it I do have 'lastName' is missing in props validation and `'invalid' is missing in props validation' errors.

After updating to 7.12.0 (and restarting VS Code) I do have the errors with the spread operator.

I found this stackoverflow question Spread operator on a component disables the react/prop-types rule on eslint-config-airbnb with a similar issue.

It seems to be related to #1939

Thanks for the help

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

No branches or pull requests

3 participants