直接上代码~~1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31export default class Parent extends Component {
render() {
return(
<div>
<Child alias={this.doSth} /><br/>
<button onClick={this.click} >父组件click</button>
</div>
)
}
doSth = (ref) => { //获取子组件的作用域
this.anything = ref
}
click = (e) => {
this.anything.myName()
}
}
class Child extends Component {
componentDidMount(){
this.props.alias(this) //子组件中的this作为参数传入
}
myName = () => alert('click me ')
render() {
return ('我是子组件')
}
}
super关键字有两种用法,既可以当成函数使用,也可以当成对象使用。在这两种情况下,它的用法完全不同。
在使用 JavaScript classes 时,你必须调用 super(); 方法才能在继承父类的子类中正确获取到类型的 this 。
1 | class A {} |
注意:
super虽然代表了父类A的构造函数,但是返回的是子类B的实例,即super内部的this指的是B,因此super()在这里相当于A.prototype.constructor.call(this)。
原文:
https://www.cnblogs.com/penghuwan/p/6707254.html
https://www.cnblogs.com/Unknw/p/6431375.html
http://www.wulv.site/2017-05-31/react-purecomponent.html
https://www.jianshu.com/p/333f390f2e84