有人可以解释为什么您必须向构造函数提供道具的关键字?我以为当您建造组件时,树从上到下工作...这意味着道具最终将在子部件中使用。
我记得在播客中听到的,凯尔·辛普森(Kyle Simpson)是"超级"关键字是一种在基于类的系统中获取相对多态参考(从方法到基类)的方法。或者据我所知,句法糖的工作方式是如何工作,因为在封面下是委派模型。
预先感谢您!
import React, {Component} from 'react'
class App extends Component {
constructor(props) {
super(props)
this.state = {
warpDriveCapacity : 10
}
}
}
如果要在构造函数中使用" this.props",则需要将道具传递给超级。否则,这并不重要,因为react sep seb sele。调用构造函数后立即从外部对实例进行。
。例如:
export class ComponentName extends Component {
constructor(props) {
super(props);
this.state = {
stateKey: props.initialProps
};
}
//...
}