Question: using vue beforeDestroy and Destroy hook doesn't work
os-kingsley opened this issue · 2 comments
I added eventListener to the created hook which works ok then I want to remove the event when the component is destroyed or beforeDestroy. But it does not work. Below is my code
created(){
this.navigation.addListener(
'didFocus',
payload => {
console.log('focus')
BackHandler.addEventListener("hardwareBackPress", this.backAction)
}
);
},
beforeDestroy(){
this.navigation.addListener(
'didBlur',
payload => {
console.log('blur')
BackHandler.removeEventListener("hardwareBackPress", this.backAction)
}
);
},
How can I remove the event when the user leaves the component?... I am implementing custom backHandler with react navigation events when the component is focused or blurred.
@Corymillz Since you need to remove the event listener when the user navigates away, you should add the navigation listener in created
.
If the listeners did not depend on navigation, you could simply attach them in created
and remove them in beforeDestroy
, without using navigation.addListener
.
Although the beforeDestroy
method works in Vue Native, destroyed
is not supported and will not be called if you add it to your component.
Please reopen in case the issue is not resolved