prop-types does not work with static propTypes
See original GitHub issueApparently, 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:
- Created 8 years ago
- Reactions:1
- Comments:16 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can always use ES6 getters, so the class becomes
Actually there is a typo in your code, the static property declaration must be followed by a
=
, not a:
.The fixed class:
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