javascript - 如何解决 react 原生 EventEmitter 监听器警告

标签 javascript node.js reactjs react-native eventemitter

我正在使用事件发射器在 map 组件和工具栏之间进行通信。注意*我在我的应用程序的其他部分使用相同的代码没有问题。我得到的错误是:

Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.

我试图通过类似的帖子来解决这个问题,但它不起作用。我认为这与两个组件中的 mount && unmount 方法有关?

工具栏组件

        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
        })
}

map 组件

  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;
    }
  }

最佳答案

唯一的问题是您的事件监听器没有被删除,因为 componentWillUnmount 方法的名称不正确。在您的代码中,mountM 是大写字母,应该是小写字母。

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

关于javascript - 如何解决 react 原生 EventEmitter 监听器警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37328255/

相关文章:

javascript - 如何在 Mac 上的 Chrome 中呈现 Mac 颜色选择器(滴管)?

javascript - 更改 window.location.href 会中止 AJAX

node.js - 如何在 Zapier 上将电子邮件地址转换为 MD5 哈希值?

javascript - 向创建的对象添加属性(Firebase 函数)

javascript - 升级到 NextJS 9.0 后如何修复 React Hooks?

javascript - 在屏幕阅读器可以使用 JavaScript 或 CSS 阅读之前删除网站上的内容...?

javascript - 如何在JavaScript中附加字符串并在同一行中预增量

node.js - 在 ElectronJS 中使用 Electron-Remote 开 Jest 测试 React 组件

javascript - 如何在 Draft.js 中对齐文本

node.js - 使用 Create React App 进行两种方式的值更改