从文档中学习React 并遇到以下示例:
class Square extends React.Component {
constructor() {
super();
this.state = {
value: null,
};
}
...
}
根据Mozilla的说法,super允许您this在构造函数中使用。是否有其他原因可以单独使用super(我知道super您也可以访问父类的方法),但是使用React时,是否还有其他单独使用的用例super()?
要使用
this关键字,我们应该在super关键字之前使用它。为什么?super用于调用父类的constructor。现在为什么我们需要打电话给父母的
constructor?答案是初始化我们通过this关键字引用的属性值。