<!DOCTYPE html>
<html>
<head>
<script src="../build/react.js"></script>
<script src="../build/react-dom.js"></script>
<script src="../build/browser.min.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/babel">
var data = 123;
var MyTitle = React.createClass({
getDefaultProps : function(){
return {
title:'hello world'
};
},
propTypes: {
title: React.PropTypes.string.isRequired,
},
render: function() {
return <h1> {this.props.title} </h1>;
}
});
ReactDOM.render(
<MyTitle title={data} />,
document.getElementById('example')
);
</script>
</body>
</html>
01、组件类的PropTypes 属性,用来验证组件实例的属性是否符合要求
001、上面的MyTitle 组件有一个title 属性。PropTypes 告诉React ,这个
title 属性是必须的,而且它的值必须是字符串。
02、组件类的getDefaultProps 方法 可以用来设置组件属性的默认值。