USING BABEL 7
As of babel v7.1.0, to use static class properties as well as properties declared with the property initializer syntax, you need to install a plugin as below:
npm install --save-dev @babel/plugin-proposal-class-properties
Also, all babel's yearly presets were deprecated, so instead of using es2015
simply install and use @babel/preset-env
alongside @babel/preset-react
. Your babel configuration should look as below:
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [["@babel/plugin-proposal-class-properties", { "loose": true }],]
}
Read more about the plugin here.
With the above installations and configurations, you can rewrite your component as below:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
export default class App extends Component {
static propTypes = { name: PropTypes.string.isRequired };
render() {
return (
<div>Static Class Properties</div>
);
}
}
And voila it works!!
NOTE: PropTypes
is not in react
package anymore, you need to import it separately from prop-types
package.