首页 > 解决方案 > React-Native-Navigation 摆脱构造函数?

问题描述

react-native-navigation(not react-navigation) 示例中说

constructor(props) {
  super(props);
  Navigation.events().bindComponent(this);
  this.state = {
    text: 'nothing yet'
  };
}

在这种情况下有没有办法摆脱构造函数?

标签: reactjsreact-nativereact-native-navigation

解决方案


在您的构造函数中基本上发生了三件事——调用超类构造函数、调用Navigation.events().bindComponent和初始状态。

如果你没有构造函数,super构造函数仍然会被调用,你可以将状态初始化移到外面,像这样:

  state = {
    text: 'nothing yet'
  };

Navigation.events().bindComponent但是,只要在没有构造函数的情况下创建类的实例时,就没有好的方法可以调用。所以如果你需要调用这个函数,你需要一个构造函数。


推荐阅读