We are working on React Native project. In that, We are showing tabbar in that and also sidebar too. So, for that side bar we added react-navigation library. But, In, Android, If user tap on that device's back button, if the drawer is opens, We have to close it.
So, We are adding addListener in componentDidMount()
and removing it in componentWillUnmount()
.
But, The issue is, If I switch to another tab and come back to previous tab, And if we tap on device back button, The back button handler not calling due to listener is removed.
Is there any alternative which method will call always once we switched to previous screen.
We know, componentDidMount only will call once while launching time of that screen.
We know we can call render method, But, We are expecting to call it in with good practice.
And is there any way to make it global way instead of writing call the classes closing drawer.
Code:
componentDidMount() {
BackHandler.addEventListener('backTapped', this.backButtonTap);
}
componentWillUnmount() {
BackHandler.removeEventListener('backTapped', this.backButtonTap);
}
backButtonTap = () => {
navigation.dispatch(DrawerActions.closeDrawer());
}
Any suggestions?