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命令生成对象实例时自动调用该方法.并且,该方法是类中必须有的 ...
随机推荐
- Numpy | ndarray数组基本操作
搞不懂博客园表格的排版... 说明: 0 ndarray :多维数组对象 1 np :import numpy as np 2 nda :表示数组的名称 1 生成数组 函数名 描述 np.array ...
- Feign 不能注入报错及接口参数问题
无法实例 解决方案: @EnableFeignClients(basePackages = "com.test.test.service") 要指定路径, 如果有设置@Compon ...
- js及jsp.java查错的几种方式
一.js 1.console.log("你想输出的内容"); 2.alert("你想输出的内容"); 3.debugger;(记得打开F12) 4.快速找到js ...
- sencha Architect 3.2及以下版本都适用的 破解方法
找到 没有的话 打开隐藏文件夹 C:\Users\ll\AppData\Local\Sencha\Sencha Architect 3.2 用编辑器 打开user.license 把 Print 修改 ...
- JUC—Callable接口
一.callable接口是什么? 面试题: 获得多线程的方法几种? 正确答案如下: 传统的 是继承thread类和实现runnable接口, java5以后又有实现 callable接口 和 java ...
- Python 排序---sort与sorted学习(这是摘录别人的资源总结,自己可临摹学习)
第一种:内建方法sort() 可以直接对列表进行排序 用法: list.sort(func=None, key=None, reverse=False(or True)) 对于reverse这个boo ...
- 转载:android audio flinger
https://blog.csdn.net/innost/article/details/6142812 https://blog.csdn.net/zyuanyun/article/details/ ...
- 题解 SP27102/UVA1747 【Swap Space】
SP27102 [Swap Space] 双倍经验:UVA1747 Swap Space 用(a,b)表示每个硬盘的原容量和新文件系统下的容量.分两种情况考虑:a≤b和a>b 第一类a≤b格式化 ...
- 剑指offer 面试题40. 最小的k个数
O(N)划分法,注意这个方法会改变原数据(函数参数是引用的情况下)!当然也可以再定义一个新容器对其划分 要求前k小的数,只要执行快排划分,每次划分都会把数据分成大小两拨.直到某一次划分的中心点正好在k ...
- vs2008编译错误fatal error C1902: 程序数据库管理器不匹配;请检查安装解决
重装了本本上的Xp系统,如往常一样,升级,装杀毒软件,开发工具.一些进行的非常顺利.然而,在我打开VS2008准备耕作的时候,尽然出现了一邪恶的错误提示:vs2008编译错误fatal error C ...