51 lines
1.3 KiB
HTML
51 lines
1.3 KiB
HTML
<!--
|
||||
|
|
* @Author: chenxuan
|
|||
|
|
* @Date: 2022-01-01 20:04:49
|
|||
|
|
* @LastEditTime: 2022-01-01 22:41:50
|
|||
|
|
* @LastEditors: chenxuan
|
|||
|
|
-->
|
|||
|
|
<!DOCTYPE html>
|
|||
|
|
<html>
|
|||
|
|
<head>
|
|||
|
|
<meta charset="utf-8">
|
|||
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|||
|
|
<title>props限制和默认值</title>
|
|||
|
|
</head>
|
|||
|
|
<body>
|
|||
|
|
<div id="app"></div>
|
|||
|
|
<script src="../package/old/react.development.js"></script>
|
|||
|
|
<script src="../package/old/react-dom.development.js"></script>
|
|||
|
|
<script src="../package/old/babel.min.js"></script>
|
|||
|
|
<script src="../package/old/prop-types.js"></script>
|
|||
|
|
<script type="text/babel">
|
|||
|
|
class Person extends React.Component{
|
|||
|
|
static propTypes = {
|
|||
|
|
name:PropTypes.string.isRequired, //限制name必传,且为字符串
|
|||
|
|
sex:PropTypes.string,//限制sex为字符串
|
|||
|
|
age:PropTypes.number,//限制age为数值
|
|||
|
|
speak:PropTypes.func
|
|||
|
|
}
|
|||
|
|
static defaultProps={
|
|||
|
|
name:"xxxx",
|
|||
|
|
sex:"xxxx",
|
|||
|
|
age:999
|
|||
|
|
}
|
|||
|
|
render(){
|
|||
|
|
const {name,sex,age}=this.props
|
|||
|
|
return(
|
|||
|
|
<ul>
|
|||
|
|
<li>姓名:{name+1}</li>
|
|||
|
|
<li>性别:{sex}</li>
|
|||
|
|
<li>年龄:{age}</li>
|
|||
|
|
</ul>
|
|||
|
|
)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function speak(){
|
|||
|
|
console.log('我说话了');
|
|||
|
|
}
|
|||
|
|
ReactDOM.render(<Person name="陈轩" age={18} sex="男" speak={speak}/>,document.getElementById('app'))
|
|||
|
|
</script>
|
|||
|
|
</body>
|
|||
|
|
</html>
|