在学习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. 三剑客之grep

    简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...

  2. thinkphp 无限极 评论

    郑创 今天用啦一天的时间用了各种方法终于把评论成无限极了,随便评论,有判断自己不能评论自己,下面先说前台源代码! 要实现的视图 前台源代码html模板 <div class="wen_ ...

  3. web.config 加密/解密 正确版

    一般加密方式: 利用Aspnet_regiis: cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 aspnet_regiis -pe "co ...

  4. vins-mono调试教程

    https://www.cnblogs.com/Jessica-jie/p/6950604.html

  5. python字典查询功能

    def fetch(data): print(' 查询功能') print('用户数据是',data) def add(): pass def change(): pass def delete(): ...

  6. Java并发编程的4个同步辅助类(CountDownLatch、CyclicBarrier、Semphore、Phaser)

    我在<jdk1.5引入的concurrent包>中,曾经介绍过CountDownLatch.CyclicBarrier两个类,还给出了CountDownLatch的演示案例.这里再系统总结 ...

  7. mo +离散化 HDU3333(听说还有离线线段树的做法 )

    http://acm.hdu.edu.cn/showproblem.php?pid=3333 mo套map会T,卡了一个logN,所以要先离散化处理 #define _CRT_SECURE_NO_WA ...

  8. Spark安装部署(local和standalone模式)

    Spark运行的4中模式: Local Standalone Yarn Mesos 一.安装spark前期准备 1.安装java $ sudo tar -zxvf jdk-7u67-linux-x64 ...

  9. vim操作表

  10. browse-agent type and curl post

    https://www.jb51.net/web/499127.html http://www.atool.org/useragent.php  query for type 用Curl测试POST ...