There is removeCurrentListener
, but no removeListener
method.
I found the answer myself.
https://github.com/facebook/react-native/blob/235b16d93287061a09c4624e612b5dc4f960ce47/Libraries/vendor/emitter/EventEmitter.js
addListener
returns a EmitterSubscription
instance that extends EventSubscription
that has remove
method.
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
Actually it does (unless I'm misunderstanding your question).
Here's how I do it:
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With