三大属性:state props refs
是组件实例
的三大属性
而函数式组件,没有实例对象,就不存在三大属性,但是他有一个属性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>函数式组件使用props</title>
</head>
<body>
<div id="test"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://cdn.staticfile.org/prop-types/15.6.1/prop-types.js"></script>
<script type="text/babel">
function Person(props) {
const { name, sex, age } = props
return (
<ul>
<li>姓名: { name }</li>
<li>性别: { sex }</li>
<li>年龄: { age }</li>
</ul>
)
}
Person.propTypes = {
name: PropTypes.string.isRequired,
sex: PropTypes.string,
age: PropTypes.number,
speak: PropTypes.func
}
Person.defaultProps = {
sex: 'boy',
age: 19
}
ReactDOM.render(<Person name="tom"/>, document.getElementById('test'))
</script>
</body>
</html>