(精华2020年5月22日更新) react基础篇 父组件传值到子组件prop的使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>prop</title>
</head>
<body>
    <div id="app">

    </div>
    <script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.7.2/prop-types.min.js"></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <script type="text/babel">
        // 复杂的组件 es6 class
        class Complex extends React.Component{
            constructor(props){
                super(props)
            }
            render(){
                // 通过this.props 来接收
                console.log(this.props);
                // this.props.hobby = '打篮球'
                // this.props = {}
                const {name,sex,age} = this.props
                return (
                    <div>
                        <h1>姓名:{name}</h1>
                        <h1>性别:{sex}</h1>
                        <h1>年龄:{age}</h1>
                    </div>
                )
            }
        }
        const info = {
            name:'张三',
            // sex:'男',
            // age:18
        }
        Complex.propTypes = {
            name:PropTypes.string.isRequired
        }
        Complex.defaultProps = {
            sex:'男',
            age:19
        }
        // 定义一个人员信息 信息从外部传入
        // 1 姓名必须指定 2 性别没有指定的话 默认为男 3年龄没有指定的话 默认为18
        ReactDOM.render(<Complex {...info}/>,document.getElementById("app"))
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值