'How to remove event listener or emitter listeners in React Native
I am trying to remove an event listener. The old way is deprecated. We are told to use .remove(). I am not sure how to refactor my code to do so. I am using a class component.
Can you provide an example of how to remove event listener. Currently my event listener is inside of another function. I have it that way as I may need to create another event listener every time the button is press and I need to remove the listener after the function runs.
startProcess = (result) => {
// stuff for running process
console.log("your function is running its process");
//deprecated but easy way to remove the event listener below
// eventEmitter.removeListener('PreparePaywallFinished', onPreparePaywallFinished);
//new way to remove event listen use remove() method on the EventSubscription returned by addEventListener()
this.subscribeStartProcess.remove;
};
handleBtnPress = () => {
// the listener
eventEmitter.addListener("onMidiStart", this.startProcess);
// emitter
NativeModules.midiBridgeStart(true, 2);
};
render(){
return <Button title='press' onPress={()=> handleBtnPress() />
}
Solution 1:[1]
You can use
window.removeEventListener('onMidiStart', handleonMidiStart);
the second argument in the removeEventListener is a variable to identify which event listed is being removed . A detailed description is being given here enter link description here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Manu Jo Varghese |
