2016-05-19 9 views
10

私はイベントコンポーネントを使用して、マップコンポーネントとツールバーの間で通信しています。注*私はこの同じコードを問題なくアプリ内の他の部分で使用しています。私が得ているエラーは次のとおりです。反応するネイティブのEventEmitterListenerを解決する方法

警告:setState(...):マウントされているコンポーネントまたはマウントされているコンポーネントのみを更新できます。これは、通常、アンマウントされたコンポーネントに対してsetState()を呼び出したことを意味します。これはノーオペレーションです。定義されていないコンポーネントのコードを確認してください。

私は同様の投稿でこれを解決しようとしましたが、機能しません。私は両方のコンポーネントでマウント& &アンマウントメソッドと関係があると思った?

ツールバーコンポーネント

 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のコンソールエラーの詳細がEventEmitter.jsでこの

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

コンプリートセクションにつながる

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

答えて

6

唯一の問題は、componentWillUnmountメソッドの名前が正しくないため、イベントリスナーが削除されないことです。あなたのコードでMmountは大文字で、小文字でなければなりません。

componentWillUnmount() { 
    this.showLocateIconListener.remove(); 
    this.endTripListener.remove(); 
    this.endSubdivisionIcon.remove(); 
} 
関連する問題