import React from 'react';
import ReactDOM from 'react-dom';
let data = 123;
class MyTitle extends React.Component {
//static propTypes = {
// title: React.PropTypes.string.isRequired
//};//用这种方式则报错
render() {
return <h1> {this.props.title} </h1>;
}
}
MyTitle.propTypes = {
title: React.PropTypes.string.isRequired
};//验证不生效
ReactDOM.render(
<MyTitle title={data}/>,
document.getElementById('example')
);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
类属性不是ES6的提案,需要babel支持
http://babeljs.io/docs/plugins/transform-class-properties/
这是es7的写法,需要在babel设置的query里面加上stage-0
staticmethod而不是staticproperty=语法不对我也是这样的情况,错误提示缺少类关键转换,不知道错在那一步。
ES6 class语法创建组件,其内部只允许定义方法,而不能定义属性,class的属性只能定义在class之外。所以
propTypes要写在组件外部,getDefaultProps也要写在外部。