2016-04-27 32 views

答えて

17

私は答えを自分自身を発見しました。

https://github.com/facebook/react-native/blob/235b16d93287061a09c4624e612b5dc4f960ce47/Libraries/vendor/emitter/EventEmitter.js

addListenerremove方法を有するEventSubscriptionを拡張EmitterSubscriptionインスタンスを返します。

https://github.com/facebook/react-native/blob/235b16d93287061a09c4624e612b5dc4f960ce47/Libraries/vendor/emitter/EventSubscription.js

const emitter = new EventEmitter(); 

const subscription = emitter.addListener('eventname',() => {}); 

subscription.remove(); // Removes the subscription 
1

実際には(あなたの質問を誤解していない限り)それは行います。ここで

は、私はそれを行う方法は次のとおりです。

class Store extends EventEmitter { 
    constructor(listenerKey) { 
     super() 
     this.listenerKey = listenerKey 
    } 

    emitChange() { 
     setTimeout(() => { 
      this.emit(this.listenerKey) 
     }, 0) 
    } 

    addChangeListener(callback) { 
     this.on(this.listenerKey, callback) 
    } 

    removeChangeListener(callback) { 
     this.removeListener(this.listenerKey, callback) 
    } 
} 
関連する問題