[英]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().
我们被告知使用.remove()。 I am not sure how to refactor my code to do so.
我不确定如何重构我的代码来这样做。 I am using a class component.
我正在使用 class 组件。
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.
目前我的事件监听器在另一个 function 中。我有这种方式,因为我可能需要在每次按下按钮时创建另一个事件监听器,并且我需要在 function 运行后删除监听器。
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() />
}
You can use您可以使用
window.removeEventListener('onMidiStart', handleonMidiStart);
the second argument in the removeEventListener is a variable to identify which event listed is being removed. removeEventListener 中的第二个参数是一个变量,用于标识列出的哪个事件将被删除。 A detailed description is being given here enter link description here
此处给出了详细说明在此输入链接说明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.