在学习react的时候,其中在构造函数里面,有一个super(props),具体是什么意思呢。

其中 super语法来自es6,其语法如下:

super([arguments]);
// 调用 父对象/父类 的构造函数 super.functionOnParent([arguments]);
// 调用 父对象/父类 上的方法

我们要理解react中的super(props),,就先看一下,es6的构造函数constructor

看如下js

		class Person{
constructor(props){
console.log("参数:"+props);
console.log("初始化 Person constructor");
this.name = "Person";
}
} class Child extends Person{
getName(){
console.log("名字为:"+this.name);
}
} var child = new Child();
child.getName();

  在js中,类在 new 实例化的时候,系统会默认调用constructor函数,在 Child类中,我们没有定义构造函数,那个系统会默认有一个constructor,并且会在里面调用super();  当我们定义了构造函数之后,就使用我们定义的。所以我们自己定义的构造函数必须要调用super()对父类进行初始化。

在react中,如果不需要在 constructor里面使用 props,是可以不用写 constructor的

这个两种调用和不调用的区别,

1、如果不需要 在 constructor里面使用 this.props ,是可以不用给super传props的

2、如果不要在constructor写逻辑,仅仅是写一个super(props),实际上整个constructor都没有写的必要

3、目前react支持一种新的写法,没有constructor情况下面的初始化数据,非常方便,这种写法,阮一峰老师的es6入门文档里面有提及,详细的自己去看

  

react的super(props)的更多相关文章

  1. React中super(props)和super()以及不写super()的区别

    一.constructor()和super()的基本含义 constructor() -- 构造方法 这是ES6对类的默认方法,通过new命令生成对象实例自动调用的方法.并且,该方法是类中必须要有的, ...

  2. react中constructor和super()以及super(props)的区别。

    react中这两个API出镜率超级高,但是一直不太懂这到底是干嘛的,有什么用:今天整理一下,方便自己查看同时方便大家. 1.constructor( )-----super( )的基本含义 const ...

  3. 如何理解react中的super() super(props)

    class WebSite extends React.Component { constructor() { super(); this.state = { name: "菜鸟教程&quo ...

  4. React关于constructor与super(props)之间的相爱相杀

    我们先把菜鸟教程的一段代码拿过来分析一下.下面这段代码是用了将生命周期方法添加到类中实现时钟效果. // 将生命周期方法添加到类中 class Clock extends React.Componen ...

  5. react super() and super(props)

    subclass: subclass is a class that extends another class. 即子类. In ES2015, to use 'this' in subclasse ...

  6. 【译】为什么要写super(props)

    译注: 原文地址 https://overreacted.io/why-do-we-write-super-props/ 正文 我听说Hooks是新的热点.好笑的是,我想通过描述一些关于class组件 ...

  7. react native中props的使用

    react native中props的使用 一.props的使用 1:父组件传递的方式 在子组件中可以用this.props访问到父组件传递的值 <View> <Text> { ...

  8. React中render Props模式

    React组件复用 React组件复用的方式有两种: 1.render Props模式 2.高阶组件HOC 上面说的这两种方式并不是新的APi. 而是利用Raect自身的编码特点,演化而来的固定编码写 ...

  9. 为什么react的组件要super(props)

    https://segmentfault.com/q/1010000008340434

随机推荐

  1. ubuntu部署php7.1

    先更新本机内置的程序. sudo apt-get updatesudo apt-get upgrade 安装PHP sudo add-apt-repository ppa:ondrej/php sud ...

  2. C# chart 编程教程

    http://www.gcpowertools.com.cn/docs/ComponentOne/WinFormChart/#!Documents/_41.htm

  3. [No0000188][VCB-Studio 科普教程 2.5] 基于 PotPlayer 和 madVR 的播放器教程(已更新 XySubFilter)

    Potplayer 是高清影视常用的播放器,界面简洁,功能齐全,比 MPC-HC 和 MPC-BE 更人性化:但其默认方案十分糟糕,预设过多错误,无法正确播放 10-bit 视频,一直饱受诟病.VCB ...

  4. TensorRT优化过程中的dropout问题

    使用tensorRT之前,你一定要注意你的网络结构是否能够得到trt的支持,无论是CNN还是RNN都会有trt的操作. 例如:tf.nn.dropout(features, keep_prob),tr ...

  5. 2017年蓝桥杯省赛A组c++第5题(递归算法填空)

    /* 由 A,B,C 这3个字母就可以组成许多串. 比如:"A","AB","ABC","ABA","AACB ...

  6. [Day4]Switch语句、数组、二维数组

    1.选择结构switch (1)格式 switch (表达式){ case 目标值1: 执行语句1; break; case 目标值2: 执行语句2; break; ...... case 目标值n: ...

  7. [https][ssl] keyless SSL

    HTTP Server 集群前的负载设备,或内容审计设备等,在处理https的时候,需要用户配置提供证书. 但是考虑到安全问题,HTTP Server并不愿意把证书配置到其他设备上. 这个时候,就有个 ...

  8. day4_处理json

    说明:#json是一种通用的数据类型,所有的语言都认识.#k - v {}#json串就是一个字符串,不能根据key-value取值#json可以转成字典#json串就是字符串,可放在三引号中校验js ...

  9. LeetCode 283 Move Zeroes 解题报告

    题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...

  10. tensorflow入门笔记(一) tf.app.flags.FLAGS

    tf.app.flags.DEFINE_xxx()就是添加命令行的optional argument(可选参数),而tf.app.flags.FLAGS可以从对应的命令行参数取出参数.举例如下: FL ...