javascript - [React Native]:找不到变量:DeviceEventEmitter
问题描述
我正在尝试使用事件侦听器来指示何时打开/关闭视图(即关闭导航),但我似乎无法引用 DeviceEventEmitter 变量。在componentWillMount上似乎没问题,但是当实际导航发生时崩溃(在componentWillUnmount上)
我看过一些对 RTCDeviceEventEmitter 和 EventListener 的引用,但我一直找不到具体的解决方案。
import { Text, View, TouchableOpacity, DeviceEventEmitter } from 'react-native';
constructor(props) {
super(props);
this.state = {
currentRequest: "",
displayRequest: false
}
}
componentWillMount() {
DeviceEventEmitter.addListener(
NavigationService.NavigationEvents.Will,
this.hideRequestLog
);
console.log('mounted');
}
componentWillUnmount() {
DeviceEventEmitter.removeListener(
NavigationService.NavigationEvents.Will,
this.hideRequestLog
);
console.log('unmounted');
}
hideRequestLog() {
this.setState({
displayRequest: false
})
}
解决方案
推荐阅读
- php - 时区显示不正确的位置
- java - 使用带有 java 的 for 循环计算给定字符串中字符的出现次数
- session - 为 express.js 中的每个请求创建多个会话
- spring - DAO 的 SpringBootTest JUnit 测试失败
- laravel - 调用没有选项名称的 Artisan 命令
- python - git push heroku master 如何将 Heroku 上的 Django 应用程序从 git deploy 迁移到 container deploy?
- javascript - 从 json 响应中获取键值
- c - C读取文本文件
- amazon-web-services - 从 s3 复制到 hdfs 时 distcp 失败
- machine-learning - 如何使用强化学习进行产品推荐?