在react-native-navigation(而不是react-navigation)中,示例如下
constructor(props) {
super(props);
Navigation.events().bindComponent(this);
this.state = {
text: 'nothing yet'
};
}在这种情况下,有没有办法摆脱构造函数呢?
发布于 2019-11-24 07:20:26
构造函数中本质上有三件事--对超类构造函数的调用、对Navigation.events().bindComponent的调用和初始状态。
如果没有构造函数,仍然会调用super构造函数,并且可以将状态初始化移到外部,如下所示:
state = {
text: 'nothing yet'
};但是,在没有构造函数的情况下创建类的实例时,没有好的方法来调用Navigation.events().bindComponent。所以如果你需要调用这个函数,你需要一个构造函数。
https://stackoverflow.com/questions/59013165
复制相似问题