<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<script src="prop-types.js"></script>
<title>Document</title>
</head>
<body>
<div id="app1"></div>
<div id="app2"></div>
<script type="text/babel">
class Person extends React.Component{
//对标签属性进行类型,必要性的限制
static propTypes={
name:PropTypes.string.isRequired, //限制name必传,且为字符串
sex:PropTypes.string,
age:PropTypes.number
}
//指定默认标签属性值
static defaultProps={
sex:"男", //sex默认值为男
age:18 //age默认值为18
}
render(){
const {name,age,sex}=this.props
return (
<ul>
<li>姓名:{name} </li>
<li>性别:{sex} </li>
<li>年龄:{age} </li>
</ul>
)
}
}
ReactDOM.render(<Person name="tom" sex="男" age={18} />,document.getElementById("app1"))
const p= {name:"王萍",sex:"女",age:19}
ReactDOM.render(<Person {...p} />,document.getElementById("app2"))
</script>
</body>
</html>