reactjs - React-Native-Navigation 摆脱构造函数?
问题描述
在react-native-navigation
(not react-navigation
) 示例中说
constructor(props) {
super(props);
Navigation.events().bindComponent(this);
this.state = {
text: 'nothing yet'
};
}
在这种情况下有没有办法摆脱构造函数?
解决方案
在您的构造函数中基本上发生了三件事——调用超类构造函数、调用Navigation.events().bindComponent
和初始状态。
如果你没有构造函数,super
构造函数仍然会被调用,你可以将状态初始化移到外面,像这样:
state = {
text: 'nothing yet'
};
Navigation.events().bindComponent
但是,只要在没有构造函数的情况下创建类的实例时,就没有好的方法可以调用。所以如果你需要调用这个函数,你需要一个构造函数。
推荐阅读
- php - pdf到php中的字节数组转换
- c++11 - ld:未找到体系结构 x86_64 的符号 clang:错误:链接器命令失败,退出代码为 1
- angular - 通过代理配置从 Electron-Angular 项目调用 API
- python - 为什么 reshape 对 Transpose 的工作方式不同并返回一个新的 nd 数组?
- android - Android的FFmpeg GL转换命令
- ios - 是否可以在文本字段中显示之前修改自动填充字符串?
- ethereum - 如何使用 web3 解码日志并取回事件?
- python - 如何使用 alpha 通道为 png 图像着色?
- java - 无法使用 gson 和改造从对象读取数据
- entity-framework - 实体框架更新种子数据——数据运动迁移