核心提示:1、react的组件中的super(props)是干啥的a、调用super的原因:在ES6中,在子类的constructor中必须先调用super才能引用this,这是因为子类没有自己的this对象,...
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); } }