首页 > 解决方案 > [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
    })
}

标签: javascriptreact-nativedeviceeventemitter

解决方案


推荐阅读