1、react的組件中的super(props)是幹啥的
a、調用super的原因:在ES6中,在子類的constructor中必須先調用super才能引用this,這是因爲子類沒有自己的this對象,而是繼承父類的this對象,然後對其進行加工,如果子類沒有定義constructor方法,這個方法會被默認添加:es6入門
b、super(props)的目的:在constructor中可以使用this.props:只有一個理由需要傳遞props作爲super()的參數,那就是你需要在構造函數內使用this.props–stackOverflow–注意:無論有沒有constructor,在render中this.props都是可以使用的,這是React自動附帶的,所以,如果在子組件中不需要用到constructor,是可以不寫的
總結:在在render函數以外要使用this,就得調用super()方法,
class ColorPoint extends Point {
}
// 等同於
class ColorPoint extends Point {
constructor(...args) {
super(...args);
}
}