zoukankan      html  css  js  c++  java
  • react 组件的构造函数

    constructor 函数时组件最先执行的函数

    class childen extends react.Component{
       constructor(props){
          super(props);
          this.state={
              attr1:"",
          }
      }
    }

    一般在constructor函数中都会存在 上述方法,个人对其的理解

    constructor():子类继承父类时的构造方法,主要时用以定义this.属性 在react中一些默认的数据可以直接在此处定义

    import React from 'react';
    
    class Mine extends React.Component {
        constructor(a) {
            super(a);
            this.state = {
                date: new Date().getTime()
            }
            //定义custom 和state并没有什么本质的区别,在都可以通过this都访问,但是推荐使用state 因为在react中 即使我们不定义this.state
            //this下仍会有一个默认的state属性,具体作用暂时没有发现,感兴趣的同学可以深入了解一下
            this.custom = {
                date: new Date().getTime()
            }
            console.log(a);
        }
    
        componentWillMount() {
            console.log("在渲染前调用",this)
        }
    
        render() {
            return (
                <div>
                    <div className='head'>
                        <span>{this.state.date}</span>
                       <span>{this.custom.date}</span>
                    </div>
                </div>
            )
        }
    }

    spuer(): 注意在定义组件的时候可以没用constructor方法,一旦定义,就必须使用spuer方法,这不是react规定的而是es6要求,具体原因如下

  • 相关阅读:
    kafka概述
    Spark网络通信分析
    spark序列化及MapOutputTracker解析
    spark checkpoint详解
    深入理解spark streaming
    spark Listener和metrics实现分析
    Spark SQL catalyst概述和SQL Parser的具体实现
    spark block读写流程分析
    java 分布式实践
    单元测试ppt
  • 原文地址:https://www.cnblogs.com/wrhbk/p/11320255.html
Copyright © 2011-2022 走看看