Skip to content

[prop-types] Throw is missing in props validation when reassign this.props to props #1257

Closed
@hellocreep

Description

Hello!

Got an strange behavior when assign this.props to props, it will throw an error message is missing in props validation.
And seems it only happens when use const props = this.props.

eslint: 4.0.0
eslint-plugin-react: 7.1.0

This piece of code will throw an error.

const Card = React.createClass({
  propTypes: {
    isReturning: PropTypes.bool,
    date: PropTypes.string
  },
  render() {
    const props = this.props;
    return (
      <div>
          {props.isReturning}
          {props.departureDate}
      </div>
    )
  }
});
error  'isReturning' is missing in props validation    react/prop-types
error  'departureDate' is missing in props validation    react/prop-types

This is no error.

const Card = React.createClass({
  propTypes: {
    isReturning: PropTypes.bool,
    date: PropTypes.string
  },
  render() {
    const {isReturning, departureDate} = this.props;
    return (
      <div>
          {isReturning}
          {departureDate}
      </div>
    )
  }
});

This is no error.

const Card = React.createClass({
  propTypes: {
    isReturning: PropTypes.bool,
    date: PropTypes.string
  },
  render() {
    return (
      <div>
          {this.props.isReturning}
          {this.props.departureDate}
      </div>
    )
  }
});

Even this is no error.

const Card = React.createClass({
  propTypes: {
    isReturning: PropTypes.bool,
    date: PropTypes.string
  },
  render() {
    const test = this.props;
    return (
      <div>
          {test.isReturning}
          {test.departureDate}
      </div>
    )
  }
});

Activity

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

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions