I am new to react-native. I have two screens screen A and screen B. I register BackHandler event listener in screen A and want to remove the listener on screen B. How can I do it.
I tried the following code.
class ScreenA extends React.Component{
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}
handleBackPress = () => {
return true;
}
_navigateToB = () => {
this.props.navigation.navigate('ScreenB')
}
render(){
return <View><Botton onPress={ () => { this._navigateToB } } /></View>
}
}
// second screen
class ScreenB extends React.Component{
componentDidMount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress )
//BackHandler.removeEventListener('hardwareBackPress', () => true ) //I tried it too
//BackHandler.removeEventListener('hardwareBackPress', true ) //I tried it too
}
handleBackPress = () => {
return true;
}
}
But this code is not removing the backHandle event listener.
and when I put the BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress )
in _navigateToB of screen A then it is working fine. Can I remove this listener on the screen B