React native DeviceEventEmitter it';他听了两遍了

React native DeviceEventEmitter it';他听了两遍了,react-native,React Native,我不知道为什么当我触发一个事件时,设备eventemitter.addListener会发出一次,但会监听两次 我在每个屏幕上都添加了一个组件Base,比如 <View> {this.props.children} <ModalComponent /> </View> 调用Service.show时,第一个日志只显示一次,但立即在addListener中显示两次 我已经试过了 this.listener = DeviceEventEmitte

我不知道为什么当我触发一个事件时,设备eventemitter.addListener会发出一次,但会监听两次

我在每个屏幕上都添加了一个组件Base,比如

<View>
    {this.props.children}
    <ModalComponent />
</View>
调用Service.show时,第一个日志只显示一次,但立即在addListener中显示两次

我已经试过了

this.listener = DeviceEventEmitter.addListener(...)
this.listener.remove()

但它给了我同样的问题

除此之外,在同一时刻,模态被复制,当我关闭时,我有两个模态要关闭


我还尝试在一个没有父组件的新屏幕上加载所有这些内容,看看这是否是问题所在,没有。它仍然存在。

我在socket.io中触发/注册两次事件时遇到了同样的问题,我的问题是因为我在DidMount方法上添加了eventListeners。但由于我的组件被多次装载,它也多次添加eventListeners


我猜您多次使用同一组件,因此多次添加同一eventListener。尝试将您的eventsListener添加到另一个只调用一次的位置。

今天,我也遇到了这个问题。我看看源代码js。我发现
DeviceEventEmit.addListener
实际上会调用
EventSubscriptionVendor.addSubscription方法

_subscriber: EventSubscriptionVendor;
addListener(
    eventType: string,
    listener: Function,
    context: ?Object,
  ): EmitterSubscription {
    return (this._subscriber.addSubscription(
      eventType,
      new EmitterSubscription(this, this._subscriber, listener, context),
    ): any);
  }

 addSubscription(
    eventType: string,
    subscription: EventSubscription,
  ): EventSubscription {
    invariant(
      subscription.subscriber === this,
      'The subscriber of the subscription is incorrectly set.',
    );
    if (!this._subscriptionsForType[eventType]) {
      this._subscriptionsForType[eventType] = [];
    }
    const key = this._subscriptionsForType[eventType].length;
//here is the point
this._subscriptionsForType[eventType].push(subscription);
    subscription.eventType = eventType;
    subscription.key = key;
    return subscription;
  }

在内部方法中,它将侦听器的argus函数推送到一个数组中;当我们多次调用它时,它将推动许多侦听器的函数


因此,在项目中,我们必须避免多次调用它,并且在卸载组件后,我们必须将其删除。

成功了!但是这与基本组件的逻辑相同。它在底座内部,底座在屏幕上只存在一次。那么@kittu88,我使用的是
react导航
,问题是因为我有两个导航器,
StackNavigator
DrawerNavigator
;因此,我开始只使用
DrawerNavigator
。很高兴它起到了作用,我认为最好的办法是将事件创建集中在应用程序中的某个位置,比如在根组件的DidMount上,以确保它不会被注册多次,还可以利用
hasListeners('eventName'))
检查注册了多少个侦听器,如果有多个侦听器,则将其清除
this.listener = DeviceEventEmitter.addListener(...)
this.listener.remove()
this.onModalVisible.bind(this)
_subscriber: EventSubscriptionVendor;
addListener(
    eventType: string,
    listener: Function,
    context: ?Object,
  ): EmitterSubscription {
    return (this._subscriber.addSubscription(
      eventType,
      new EmitterSubscription(this, this._subscriber, listener, context),
    ): any);
  }

 addSubscription(
    eventType: string,
    subscription: EventSubscription,
  ): EventSubscription {
    invariant(
      subscription.subscriber === this,
      'The subscriber of the subscription is incorrectly set.',
    );
    if (!this._subscriptionsForType[eventType]) {
      this._subscriptionsForType[eventType] = [];
    }
    const key = this._subscriptionsForType[eventType].length;
//here is the point
this._subscriptionsForType[eventType].push(subscription);
    subscription.eventType = eventType;
    subscription.key = key;
    return subscription;
  }