zoukankan      html  css  js  c++  java
  • React---组件实例三大核心属性(二)props

    一、 理解

    1. 每个组件对象都会有props(properties的简写)属性
    2. 组件标签的所有属性都保存在props
    3. 通过标签属性从组件外向组件内传递变化的数据

    二、作用

    1. 注意: 组件内部不要修改props数据
    2. 内部读取某个属性值

    三、编码操作

    1. 内部读取某个属性值

        this.props.name

        2. 对props中的属性值进行类型限制必要性限制

        第一种方式(React v15.5 开始已弃用):

    Person.propTypes = {
     name: React.PropTypes.string.isRequired,
     age: React.PropTypes.number
    }

        第二种方式(新):使用prop-types库进限制(需要引入prop-types库)

    Person.propTypes = {
      name: PropTypes.string.isRequired,
      age: PropTypes.number
    }

      3. 扩展属性: 将对象的所有属性通过props传递

        <Person {...person}/>

      4. 默认属性值

    Person.defaultProps = {
      age: 18,
      sex:'男'
    }

      5. 组件类的构造函数

    constructor(props){
      super(props)
      console.log(props)//打印所有属性
    }

    三、案例

      1. 类式组件使用props

    <script type="text/babel">
            //创建组件
            class Person extends React.Component{
    
                constructor(props){
                    //构造器是否接收props,是否传递给super,取决于:是否希望在构造器中通过this访问props
                    // console.log(props);
                    super(props)
                    console.log('constructor',this.props);
                }
    
                //对标签属性进行类型、必要性的限制
                static propTypes = {
                    name:PropTypes.string.isRequired, //限制name必传,且为字符串
                    sex:PropTypes.string,//限制sex为字符串
                    age:PropTypes.number,//限制age为数值
                }
    
                //指定默认标签属性值
                static defaultProps = {
                    sex:'男',//sex默认值为男
                    age:18 //age默认值为18
                }
                
                render(){
                    // console.log(this);
                    const {name,age,sex} = this.props
                    //props是只读的
                    //this.props.name = 'jack' //此行代码会报错,因为props是只读的
                    return (
                        <ul>
                            <li>姓名:{name}</li>
                            <li>性别:{sex}</li>
                            <li>年龄:{age+1}</li>
                        </ul>
                    )
                }
            }
    
            //渲染组件到页面
            ReactDOM.render(<Person name="jerry"/>,document.getElementById('test1'))
        </script>

      2. 函数组件使用props

    <script type="text/babel">
            //创建组件
            function Person (props){
                const {name,age,sex} = props
                return (
                        <ul>
                            <li>姓名:{name}</li>
                            <li>性别:{sex}</li>
                            <li>年龄:{age}</li>
                        </ul>
                    )
            }
            Person.propTypes = {
                name:PropTypes.string.isRequired, //限制name必传,且为字符串
                sex:PropTypes.string,//限制sex为字符串
                age:PropTypes.number,//限制age为数值
            }
    
            //指定默认标签属性值
            Person.defaultProps = {
                sex:'男',//sex默认值为男
                age:18 //age默认值为18
            }
            //渲染组件到页面
            ReactDOM.render(<Person name="jerry"/>,document.getElementById('test1'))
        </script>

     四、展开运算符

     1 <script type="text/javascript" >
     2             let arr1 = [1,3,5,7,9]
     3             let arr2 = [2,4,6,8,10]
     4             console.log(...arr1); //展开一个数组
     5             let arr3 = [...arr1,...arr2]//连接数组
     6             
     7             //在函数中使用,参数个数不确定
     8             function sum(...numbers){
     9                 return numbers.reduce((preValue,currentValue)=>{
    10                     return preValue + currentValue
    11                 })
    12             }
    13             console.log(sum(1,2,3,4));
    14 
    15             //构造字面量对象时使用展开语法
    16             let person = {name:'tom',age:18}
    17             let person2 = {...person}
    18             //console.log(...person); //报错,展开运算符不能展开对象
    19             person.name = 'jerry'
    20             console.log(person2);
    21             console.log(person);
    22 
    23             //合并
    24             let person3 = {...person,name:'jack',address:"地球"}
    25             console.log(person3);
    26             
    27         </script>

     五、组件通信总结

      1.方式:
              props:
                  (1).children props
                  (2).render props
              消息订阅-发布:
                  pubs-sub、event等等
              集中式管理:
                  redux、dva等等
               context:
                  生产者-消费者模式
      2.组件间的关系
              父子组件:props
              兄弟组件(非嵌套组件):消息订阅-发布、集中式管理
              祖孙组件(跨级组件):消息订阅-发布、集中式管理、conText(用的少)

     

     

     

  • 相关阅读:
    【leetcode】面试题 01.04. 回文排列
    【leetcode】将数组分成和相等的三个部分
    【leetcode】杨辉三角
    【leetcode】杨辉三角 II
    【leetcode】判断路径是否相交
    【leetcode】路径总和
    【leetcode】山脉数组的峰顶索引
    053-4
    053-151
    053-272
  • 原文地址:https://www.cnblogs.com/le220/p/14668834.html
Copyright © 2011-2022 走看看