Files
react/basic/1/P08.html
T
2022-05-01 19:27:52 +08:00

51 lines
1.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!--
* @Author: chenxuan
* @Date: 2022-01-01 14:00:24
* @LastEditTime: 2022-01-01 22:27:36
* @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{
render(){
const {name,sex,age}=this.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>