限制props的类型
<!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">
<title>helloReact</title>
</head>
<body>
<div id="test"></div>
<div id="test1"></div>
<div id="test2"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script src="../js/prop-types.js"></script>
<script type="text/babel">
class Person extends React.Component{
render () {
const {name, sex, age, speck} = this.props
return (
<div>
<ul>
<li>姓名: {name}</li>
<li>性别: {sex+"性"}</li>
<li>年龄: {age}</li>
</ul>
</div>
)
}
}
Person.propTypes = {
name: PropTypes.string.isRequired,
sex: PropTypes.string,
age: PropTypes.number,
speck: PropTypes.func
}
Person.defaultProps = {
sex: '女',
age: 30
}
function speck () {
console.log('ok')
}
ReactDOM.render(<Person name="张三" sex="男"/>, document.getElementById('test'))
ReactDOM.render(<Person name="李四" age={19} />, document.getElementById('test1'))
ReactDOM.render(<Person name="王五" age={20} sex="男" speck={speck}/>, document.getElementById('test2'))
</script>
</body>
</html>
props的简写方式
<!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">
<title>helloReact</title>
</head>
<body>
<div id="test"></div>
<div id="test1"></div>
<div id="test2"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script src="../js/prop-types.js"></script>
<script type="text/babel">
class Person extends React.Component{
static propTypes = {
name: PropTypes.string.isRequired,
sex: PropTypes.string,
age: PropTypes.number,
speck: PropTypes.func
}
static defaultProps = {
sex: '女',
age: 30
}
render () {
const {name, sex, age, speck} = this.props
return (
<div>
<ul>
<li>姓名: {name}</li>
<li>性别: {sex+"性"}</li>
<li>年龄: {age}</li>
</ul>
</div>
)
}
}
function speck () {
console.log('ok')
}
ReactDOM.render(<Person name="张三" sex="男"/>, document.getElementById('test'))
ReactDOM.render(<Person name="李四" age={19} />, document.getElementById('test1'))
ReactDOM.render(<Person name="王五" age={20} sex="男" speck={speck}/>, document.getElementById('test2'))
</script>
</body>
</html>
函数式组件使用props
<!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">
<title>helloReact</title>
</head>
<body>
<div id="test"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script src="../js/prop-types.js"></script>
<script type="text/babel">
function Person (props) {
const {name, sex, age, speck} = props
return (
<div>
<ul>
<li>姓名: {name}</li>
<li>性别: {sex+"性"}</li>
<li>年龄: {age}</li>
</ul>
</div>
)
}
Person.propTypes = {
name: PropTypes.string.isRequired,
sex: PropTypes.string,
age: PropTypes.number,
speck: PropTypes.func
}
Person.defaultProps = {
sex: '女',
age: 30
}
function speck () {
console.log('ok')
}
ReactDOM.render(<Person name="张三"/>, document.getElementById('test'))
/*
1、react解析组件标签,找到MyComponent组件
2、发现组件是使用函数定义的,随后调用该函数,将返回的虚拟DOM转换为真实DOM,随后呈现在页面中
*/
</script>
</body>
</html>