question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

prop-types does not work with static propTypes

See original GitHub issue

Apparently, the prop-types rule does not recognize propTypes, which are declared using a static property on the class:

export default class Greeting extends React.Component {
  static propTypes: {
    count: React.PropTypes.number.isRequired
  }
  render () {
    return <h1>Hello, world! {this.props.count} greetings from me!</h1>;
  }
}

The following works completely fine:

export default class Greeting extends React.Component {
  render () {
    return <h1>Hello, world! {this.props.count} greetings from me!</h1>;
  }
}

Greeting.propTypes = {
  count: React.PropTypes.number.isRequired
};

I am using eslint 1.3.1 and eslint-plugin-react 3.3.0.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

41reactions
ghostcommented, Nov 9, 2016

You can always use ES6 getters, so the class becomes

export default class Greeting extends React.Component {
  static get propTypes() {
    return {
       count: React.PropTypes.number.isRequired
    }
  }
  render () {
    return <h1>Hello, world! {this.props.count} greetings from me!</h1>;
  }
}
11reactions
yannickcrcommented, Aug 31, 2015

Actually there is a typo in your code, the static property declaration must be followed by a =, not a :.

The fixed class:

export default class Greeting extends React.Component {
  static propTypes = {
    count: React.PropTypes.number.isRequired
  }
  render () {
    return <h1>Hello, world! {this.props.count} greetings from me!</h1>;
  }
}

Babel don’t throw an error but do not export the property in the transpiled code neither, so it is like you did not had any declared propTypes. With Espree, the default ESLint parser, you’ll get an Unexpected token : error.

You can compare Babel output here http://babeljs.io/repl

Read more comments on GitHub >

github_iconTop Results From Across the Web

prop-types does not work with static propTypes #203 - GitHub
Apparently, the prop-types rule does not recognize propTypes, which are declared using a static property on the class: export default class ...
Read more >
Static propTypes not working under ES6 - Stack Overflow
I have run into issues with ReactRouter where although I thought I always supplied a prop, RR would render the component without the...
Read more >
Typechecking With PropTypes - React
To run typechecking on the props for a component, you can assign the special propTypes property: import PropTypes from 'prop-types'; class Greeting extends ......
Read more >
How to validate React props using PropTypes - LogRocket Blog
Learn how to validate props with React PropTypes, React's internal mechanism for adding type checking to component props.
Read more >
How to Use PropTypes in React - freeCodeCamp
PropTypes are simply a mechanism that ensures that the passed value is of the correct datatype. This makes sure that we don't receive...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found