Подтвердить что ты не робот

Как разрешить ответное предупреждение EventEmitterListener

Я использую излучатель события для связи между компонентом карты и панелью инструментов. Примечание * Я использую этот же код в других частях моего приложения без проблем. Ошибка, которую я получаю:

Предупреждение: setState (...): Может обновлять только смонтированный или монтируемый компонент. Обычно это означает, что вы вызывали setState() на немонтированном компоненте. Это не-op. Проверьте код для компонента undefined.

Я пытался решить это с помощью подобных сообщений, но он не работает. Я думал, что это связано с mount && отключить методы в обоих компонентах?

Компонент панели инструментов

        componentDidMount() {
    this.showLocateIconListener = AppEventEmitter.addListener('isTrip', this.isTrip.bind(this));
    this.endTripListener = AppEventEmitter.addListener('showLocateIcon', this.showLocateIcon.bind(this));
    this.endSubdivisionIcon = AppEventEmitter.addListener('showSubdivisionIcon', this.showSubdivisionIcon.bind(this));
}

componentWillUnMount() {
    this.showLocateIconListener.remove();
    this.endTripListener.remove();
    this.endSubdivisionIcon.remove();
}


 //// this is where the error is happening
showSubdivisionIcon(val) {
    if (val != 0)
        this.setState({
            items: menuSubdivision,
            subdivisionId: val
        })
    else
        this.setState({
            items: menu
        })
}

Компонент карты

  onMarkerPress(val) {
    AppEventEmitter.emit('showSubdivisionIcon', val.id);
}

Детали ошибки консоли для EventEmitter.js приводят к этому

  subscription.listener.apply(
        subscription.context,
        Array.prototype.slice.call(arguments, 1)
      );

Завершить раздел в EventEmitter.js

      /**
   * Emits an event of the given type with the given data. All handlers of that
   * particular type will be notified.
   *
   * @param {string} eventType - Name of the event to emit
   * @param {...*} Arbitrary arguments to be passed to each registered listener
   *
   * @example
   *   emitter.addListener('someEvent', function(message) {
   *     console.log(message);
   *   });
   *
   *   emitter.emit('someEvent', 'abc'); // logs 'abc'
   */
  emit(eventType: String) {
    var subscriptions = this._subscriber.getSubscriptionsForType(eventType);
    if (subscriptions) {
      var keys = Object.keys(subscriptions);
      for (var ii = 0; ii < keys.length; ii++) {
        var key = keys[ii];
        var subscription = subscriptions[key];

        // The subscription may have been removed during this event loop.
        if (subscription) {
          this._currentSubscription = subscription;
          subscription.listener.apply(
            subscription.context,
            Array.prototype.slice.call(arguments, 1)
          );
        }
      }
      this._currentSubscription = null;
    }
  }
4b9b3361

Ответ 1

Единственная проблема заключается в том, что ваши прослушиватели событий не удаляются, потому что имя метода componentWillUnmount неверно. В вашем коде M of mount является капиталом, где он должен быть строчным.

componentWillUnmount() {
    this.showLocateIconListener.remove();
    this.endTripListener.remove();
    this.endSubdivisionIcon.remove();
}