class实现React继承以及constructor的super的问题
class Father {
constructor() {
this.name = 'zhangsan'
}
getDate = (data) => {
this.haha() //因为继承关系 所以这个this也指向子组件实例 所以可调用子组件的方法
}
getName() {
console.log('父组件的name')
}
}
class Son extends Father {//extends相当于继承了父组件的所有属性和方法
constructor(props) {
super(props)
console.log("this", this)
}
haha = () => {
console.log('子组件的哈哈')
}
getName = () => {
super.getName();//如果不希望自己的getName覆盖父亲组件的getName
console.log('子组件的name')
}
}
let son = new Son();
console.log('son 实例', son)
son.getDate() //子组件的哈哈
son.getName() //父组件的name 子组件的name
看到上面的代码会发现:

说明Es6里 类里面的this 指向实例对象new Son()
那么问题来啦,请问react组件Son里面的this也是完全等价于实例对象new Son()吗? 答案是否:react组件里this=new Son()实例+React包装
Button父组件:用来给子组件传递props
Father父组件
import React from 'react';
class Father extends React.Component {
constructor(props) {
super(props)
this.moduleName = 'moduleName';
}
getDate = () => {
this.haha()
}
getName() { //不能用做箭头函数 如果是箭头函数则会报错 因为自组件的方法中super.getName()中的super指向的是Father类 如果是箭头函数 指向的是Son的实例对象
console.log('父组件的name')
}
render() {
return 'father'
}
}
export default Father
Son子组件
constructor(props) {
super(props);
console.log("this", this)
}
haha = () => {
console.log('子组件的哈哈')
}
getName = () => {
super.getName();
console.log('子组件的name')
}
render() {
return <div>
<button onClick={this.getName}>点击获取name</button>
<button onClick={this.getDate}>点击获取父亲组件的getDate方法</button>
</div>
}
}
let son = new Son('我是Son的实例参数') //子组件的哈哈
console.log('Son的实例', son) //父组件的name 子组件的name
son.getName()
son.getDate()
export default Son

所以说:react中的this 等价于 new Son()+React内部方法的集合
那么问题来啦,组件的constructor方法必须要写吗? super必须要加吗?super里面的参数必须要写吗?
1.组件的constructor方法必须要写吗
如果子类没有定义constructor方法,这个方法会被默认添加,代码如下。也就是说,不管有没有显式定义,任何一个子类都有constructor方法。
class ColorPoint extends Point {
}
// 等同于
class ColorPoint extends Point {
constructor(...args) {
super(...args);
}
}
// 可见没有写constructor,在执行过程中会自动补上
2.如果constructor()方法定义啦 isuper必须要加吗? 答案是:必须要加


3.super()里面的参数必须要写吗? 答案是:可写可不写 如果想要在constructor()里面使用this.props 则需要传递参数
如果子类super() 不传递props/或者传递props
constructor(props) {
super();
console.log("子类this.props", this.props)
}
父亲类别
constructor() {
super()
console.log("父类this.props", this.props)
}

如果子类super()传递props
constructor(props) {
super(props);
console.log("子类this.props", this.props)
}
父类super()传递props
constructor(props) {
super(props)
console.log("父类this.props", this.props)
}

总结:因为父组件也是继承于 React.Component 所以要想在当前组件里在constructor()里使用this.props 则需要给他的每一层的父组件的super里面加props才能生效值
class实现React继承以及constructor的super的问题的更多相关文章
- React中类定义组件constructor 和super
刚开始学习React没多久,在老师的教程里看到了类组件的使用示例,但是和资料上有些冲突,而引发了一些疑问: 类组件中到底要不要定义构造函数constructor()? super()里边到底要不要传入 ...
- react组件中的constructor和super小知识
react组件中的constructor和super小知识 1.react中用class申明的类一些小知识 如上图:类Child是通过class关键字申明,并且继承于类React. A.Child的类 ...
- react的constructor和super的具体含义和使用
1.constructor( )-----super( )的基本含义 这是ES6对类的默认方法,通过 new 命令生成对象实例时自动调用该方法.并且,该方法是类中必须有的,如果没有显示定义,则会默认添 ...
- react中constructor和super()以及super(props)的区别。
react中这两个API出镜率超级高,但是一直不太懂这到底是干嘛的,有什么用:今天整理一下,方便自己查看同时方便大家. 1.constructor( )-----super( )的基本含义 const ...
- React关于constructor与super(props)之间的相爱相杀
我们先把菜鸟教程的一段代码拿过来分析一下.下面这段代码是用了将生命周期方法添加到类中实现时钟效果. // 将生命周期方法添加到类中 class Clock extends React.Componen ...
- class 中的 构造方法、static代码块、私有/公有/静态/实例属性、继承 ( extends、constructor、super()、static、super.prop、#prop、get、set )
part 1 /** * << class 中的 static 代码块与 super.prop 的使用 * * - ...
- JAVA继承时this和super关键字
JAVA继承时this和super关键字 本文主要讨论在方法前使用this或super关键字时,编译器在什么地方查找对应的函数. 在子类中指定this关键字.首先在本类中查找,如果本类中找不到,再在父 ...
- 继承及属性查找+super()和mro()+多态
继承及属性查找+super()和mro()+多态 一. ★继承 1. 什么是继承? 继承就是新建类的一种方式,新建的类我们称为子类或者叫派生类,被继承的类我们称为父类或者基类 子类可以使用父类中的属性 ...
- react中constructor()和super()的具体含义以及如何使用
1.constructor()---super( )的基本含义 constructor()--构造方法 这是ES6对类的默认方法,通过new命令生成对象实例时自动调用该方法.并且,该方法是类中必须有的 ...
随机推荐
- nginx反向代理https访问502, nginx反向代理, 支持SNI的https回源,SNI源点,nginx反向代理报错
正常nginx配置了SSL是可以通过HTTPS访问后端的,但是对有配置SNI + https后端的支持有点麻烦. 编译安装nginx后,看一下是否支持SNI /usr/local/nginx/sbin ...
- 解决pjax加载页面不执行js插件的问题
个人博客 地址:http://www.wenhaofan.com/article/20180913220425 介绍 在使用jquery.pjax的时候发现加载页面时不会执行其中的layui以及jqu ...
- 【转载】SpringMVC前台给后台传值的方式
转自:http://blog.csdn.net/flymoringbird/article/details/53126505 1. 基本数据类型(以int为例,其他类似): Controller代码: ...
- Jmeter-几种脚本录制方式
一.使用代理服务器进行录制 1.创建线程组 2.创建http代理服务器 分组解释 不对样本分组:所有请求全部罗列 在组件添加分隔:加入一个虚拟的以分割线命名的动作 每个组放入一个新的控制器:执行时按控 ...
- OpenCV3.0 + VS2015出现“ACCESS_MASK不明确”错误
问题:Vs 使用openCV 3.0+ 出错error C2872: “ACCESS_MASK”: 不明确的符号 环境: 系统:Win7 环境:VS2015 64bit 原因: 是因为我项目中的其中一 ...
- 测试linux是否能访问外网
方法1 curl -l http://www.baidu.com 方法2 wget http://www.baidu.com
- 载 js验证密码 必须由大小写字母、数字和特殊字符组成
转自:https://blog.csdn.net/weixin_43824935/article/details/93601064 密码长度8-16位 必须由大写字母,小写字母,数字,特殊符号组成 正 ...
- AndroidBDMap学习01:基于百度地图SDK的配置以及利用API实现一个简单的地图应用
(一)注册并获取AK码: step1:找到keytool工具,并转移到.android目录下.(前提是已经安装了java jre/jdk) 为避免有些情况,在控制台无法找到keytool,可以把与k ...
- IntelliJ IDEA 2017.3尚硅谷-----配置 Maven
- [NOIP2018(PJ)] 摆渡车
题目链接 题意 有 $n$ 个同学在等车,每位同学从某时刻开始等车,相邻两趟车之间至少间隔 $m$ 分钟.凯凯可以任意安排发车时间,求所有同学等车时间之和的最小值. 分析 这题首先能想到是动态规划 很 ...