首页 > 解决方案 > 在 react-native 模块中使用 DeviceEventEmitter.addListener

问题描述

我正在尝试通过我的自定义 react-native 模块中的 DeviceEventEmitter 接收自定义事件。我正在调用:

DeviceEventEmitter.addListener('scanNotify',(event)=>{
    console.log("scanNotify in lib", event); 
});

请注意,该代码位于模块级别,在 App 生命周期方法之外

在应用程序中,我触发了一个事件:

DeviceEventEmitter.emit('scanNotify', { message: 'from App (DeviceEventEmitter)' });;

但是模块中的监听器没有被调用。如果我将侦听器放入应用程序本身,它就可以工作。正常吗?我什至可以收听模块内的内容吗?

标签: javascriptreact-native

解决方案


推荐阅读