super()仅在具有构造函数的React组件内部被调用。例如,以下代码不需要超级:
class App extends React.component { render(){ return <div>Hello { this.props.world }</div>; }}但是,如果我们有一个构造函数,那么它
super()是强制性的:
class App extends React.component { constructor(){ console.log(this) //Error: 'this' is not allowed before super() }}之所以
this不能被允许之前的原因
super()是因为
this未
super()调用if未初始化。但是,即使不使用,
this我们也需要
super()内部构造函数,因为
ES6class constructors MUST call super if they aresubclasses。因此,
super()只要有构造函数,就必须调用。(但是子类不必具有构造函数)。
super(props)如果必须使用
this.props,我们可以在构造函数内部调用,例如:
class App extends React.component{ constructor(props){ super(props); console.log(this.props); // prints out whatever is inside props }}我希望我能说清楚。



