我正在尝试使用事件侦听器来指示何时打开/关闭视图(即,在导航中关闭),但是我似乎无法引用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
})
}