javascript - 在 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)' });;
但是模块中的监听器没有被调用。如果我将侦听器放入应用程序本身,它就可以工作。正常吗?我什至可以收听模块内的内容吗?
解决方案
推荐阅读
- javascript - 如何在 React 重新渲染之前过渡动画?
- java - JUnit 如何在 Optional 和对象上断言 True/Equals
- angular - 如何在表单数组中添加无效的表单控件而不影响其功能
- python - 使用 Scrapy 和 CSS 选择器抓取整个页面
- numpy - 其他值的 trim_zeros 的 Numpy 等价物
- vim - 如何在 Vim 中阻止箭头键和鼠标?
- excel-formula - 根据 Criteria 计算出现次数
- javascript - HTML5 Canvas 如何绘制带渐变边框的松鼠?
- java - 从对象获取 JavaArray 属性
- java - (随机选择)文件中的名称