zoukankan      html  css  js  c++  java
  • React学习(1)——constructor

    1     constructor(props) {
    2         super(props);
    3         this.state = {
    4             orderNo: "001",
    5             wid: 6
    6         };
    7     }

    constructor:

    在ES6的语法中,constructor方法是类的默认方法

    通过new命令生成对象实例时,自动调用该方法

    一个类必须有constructor方法,如果没有显式定义,一个空的constructor方法会被默认添加。

    super():

    子类没有自己的this对象,必须调用父类的this对象

    super()用于子类在constructor()中调用,继承父类的this对象

    super与super(props):

    super()super(props)的区别就是你是否需要在构造函数内使用this.props

    如果需要,则必须要写props

    如果不需要,写不写效果是一样的

    this.state={}:

    state的初始化

    props的初始化:

    1 static defaultProps = {
    2     onClick : null,
    3     className : '',
    4     text : '默认'
    5 };

    State与Props

    State主要用于更新界面,组件的State属性在生命周期函数 getInitialState中初始化,当调用组件的this.setState改变state的时候,组件会重新渲染刷新。 
    Props主要用于组件之间传递数据,也就是标签的属性 这里的pname属性就可以在MyText中通过this.props.pname得到

  • 相关阅读:
    时间形式的转换
    vue 按enter键 进行搜索或者表单提交
    关于Cookie 关于前端存储数据
    复杂数组去重
    蜜蜂
    MongoDB学习记录一
    python 基础 day03—函数
    python 基础 day03—文件操作
    python 基础 day02—列表List / 元组Tuple
    python 基础 day02—初识模块
  • 原文地址:https://www.cnblogs.com/chaoxiZ/p/9356092.html
Copyright © 2011-2022 走看看