组件实例三大属性之props的使用

  • props的使用

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>props基本使用</title>
    </head>
    <body>
        <script src="../js/react.development.js"></script>
        <script src="../js/react-dom.development.js"></script>
        <script src="../js/babel.min.js"></script>
    
        <div id="test1"></div>
        <div id="test2"></div>
    
        <script type="text/babel">
            class Person extends React.Component{
                render(){
                    console.log(this);
                    const {name,age,sex} = this.props;
                    return(
                        <ul>
                            <li>{name}</li>
                            <li>{age+1}</li>
                            <li>{sex}</li>
                        </ul>
                    )
                }
            }
            ReactDOM.render(<Person name = "sun" age = {18} sex = "woman"/>,document.getElementById("test1"))
    
            const p = {name:'yuan',age:19,sex:"man"}
            ReactDOM.render(<Person {...p}/>,document.getElementById("test2"))
    
        </script>
    </body>
    </html>
    

    运行效果
    在这里插入图片描述

  • props限制

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>对props进行限制</title>
    </head>
    <body>
        <script src="../js/react.development.js"></script>
        <script src="../js/react-dom.development.js"></script>
        <script src="../js/babel.min.js"></script>
        <!-- 引入prop-types,用于对组件标签属性进行限制 -->
        <script src="../js/prop-types.js"></script>
    
        <div id="test1"></div>
        <div id="test2"></div>
        <div id="test3"></div>
    
    
        <script type="text/babel">
            class Person extends React.Component{
                render(){
                    console.log(this);
                    const {name,age,sex} = this.props;
                    return(
                        <ul>
                            <li>{name}</li>
                            <li>{age+1}</li>
                            <li>{sex}</li>
                        </ul>
                    )
                }
            }
            // 对标签属性进行类型、必要性的限制
            Person.propTypes = {
                // 必传且为字符串
                name:PropTypes.string.isRequired,
                // 字符串
                sex:PropTypes.string,
                // 数字
                age:PropTypes.number,
                // 函数
                speak:PropTypes.func
            }
            // 对标签默认属性值设置
            Person.defaultProps = {
                sex:"男",
                age:12
            }
    
            function speak() {
                console.log('说话');
            }
    
            ReactDOM.render(<Person name = "sun" age = {18} sex = "woman"/>,document.getElementById("test1"))
            ReactDOM.render(<Person name = "feng" age = {3} speak = {speak}/>,document.getElementById("test2"))
            const p = {name:'yuan',sex:"man"}
            ReactDOM.render(<Person {...p}/>,document.getElementById("test3"))
    
        </script>
    
    </body>
    </html>
    

    运行效果
    在这里插入图片描述

  • props简写

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>props的简写方式</title>
    </head>
    <body>
        <script src="../js/react.development.js"></script>
        <script src="../js/react-dom.development.js"></script>
        <script src="../js/babel.min.js"></script>
        <!-- 引入prop-types,用于对组件标签属性进行限制 -->
        <script src="../js/prop-types.js"></script>
    
        <div id="test1"></div>
        <script type="text/babel">
            class Person extends React.Component{
                // constructor(props){
                //     super(props);
                //     console.log('props',props);
                //     console.log('this.props',this.props);
                // }
                
                // 对标签属性进行类型、必要性的限制
                static propTypes = {
                    // 必传且为字符串
                    name:PropTypes.string.isRequired,
                    // 字符串
                    sex:PropTypes.string,
                    // 数字
                    age:PropTypes.number,
                }
                // 对标签默认属性值设置
                static defaultProps = {
                    sex:"男",
                    age:12
                }
                
                render(){
                    console.log(this);
                    const {name,age,sex} = this.props;
                    return(
                        <ul>
                            <li>{name}</li>
                            <li>{age+1}</li>
                            <li>{sex}</li>
                        </ul>
                    )
                }
            }
            
            ReactDOM.render(<Person name = "sun"/>,document.getElementById("test1"))
    
        </script>
    
    </body>
    </html>
    

    运行效果
    在这里插入图片描述

  • 函数组件使用props

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>函数组件使用props</title>
    </head>
    
    <body>
        <script src="../js/react.development.js"></script>
        <script src="../js/react-dom.development.js"></script>
        <script src="../js/babel.min.js"></script>
        <!-- 引入prop-types,用于对组件标签属性进行限制 -->
        <script src="../js/prop-types.js"></script>
    
        <div id="test1"></div>
    
    
        <script type="text/babel">
            function Person(props) {
                const { name, age, sex } = props
                return (
                    <ul>
                        <li>姓名:{name}</li>
                        <li>年龄:{age + 1}</li>
                        <li>性别:{sex}</li>
                    </ul>
                )
            }
            Person.propTypes = {
                // 必传且为字符串
                name:PropTypes.string.isRequired,
                // 字符串
                sex:PropTypes.string,
                // 数字
                age:PropTypes.number,
            }
            // 对标签默认属性值设置
            Person.defaultProps = {
                sex:"男",
                age:12
            }
    
            ReactDOM.render(<Person name="sun" />, document.getElementById("test1"))
    
        </script>
    
    </body>
    
    </html>
    

    运行结果
    在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值