48 lines
1.3 KiB
HTML
48 lines
1.3 KiB
HTML
<!--
|
||
* @Author: chenxuan
|
||
* @Date: 2022-01-02 13:37:37
|
||
* @LastEditTime: 2022-01-02 15:04:53
|
||
* @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">
|
||
function Person(props){
|
||
const {name,sex,age}=props
|
||
return(
|
||
<ul>
|
||
<li>姓名:{name+1}</li>
|
||
<li>性别:{sex}</li>
|
||
<li>年龄:{age}</li>
|
||
</ul>
|
||
)
|
||
}
|
||
Person.propTypes = {
|
||
name:PropTypes.string.isRequired, //限制name必传,且为字符串
|
||
sex:PropTypes.string,//限制sex为字符串
|
||
age:PropTypes.number,//限制age为数值
|
||
speak:PropTypes.func
|
||
}
|
||
Person.defaultProps={
|
||
name:"xxxx",
|
||
sex:"xxxx",
|
||
age:999
|
||
}
|
||
function speak(){
|
||
console.log('我说话了');
|
||
}
|
||
ReactDOM.render(<Person name="陈轩" age={18} sex="男" speak={speak}/>,document.getElementById('app'))
|
||
</script>
|
||
</body>
|
||
</html> |