admin管理员组文章数量:1434900
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 ponentDidMount()
and removing it in ponentWillUnmount()
.
But, The issue is, If I switch to another tab and e 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, ponentDidMount 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:
ponentDidMount() {
BackHandler.addEventListener('backTapped', this.backButtonTap);
}
ponentWillUnmount() {
BackHandler.removeEventListener('backTapped', this.backButtonTap);
}
backButtonTap = () => {
navigation.dispatch(DrawerActions.closeDrawer());
}
Any suggestions?
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 ponentDidMount()
and removing it in ponentWillUnmount()
.
But, The issue is, If I switch to another tab and e 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, ponentDidMount 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:
ponentDidMount() {
BackHandler.addEventListener('backTapped', this.backButtonTap);
}
ponentWillUnmount() {
BackHandler.removeEventListener('backTapped', this.backButtonTap);
}
backButtonTap = () => {
navigation.dispatch(DrawerActions.closeDrawer());
}
Any suggestions?
Share Improve this question edited Apr 9, 2019 at 9:59 Anilkumar iOS Developer asked Apr 9, 2019 at 9:51 Anilkumar iOS DeveloperAnilkumar iOS Developer 3,76510 gold badges61 silver badges120 bronze badges 2- Changing the active tab should change the parent ponent's state, so you can listen for that change and reattach the listener, maybe? – user5734311 Commented Apr 9, 2019 at 9:53
- Thanks so much for your quick reply @ChrisG, But, I am looking for any other default method or some property would be there to call this as with good practice. And is there any way to make it global way instead of writing call the classes closing drawer. – Anilkumar iOS Developer Commented Apr 9, 2019 at 9:56
1 Answer
Reset to default 1I would suggest to use react-navigation's own Navigation Lifecycle listener, so you can also handle different back button behaviour on different page.
ponentDidMount() {
this.willFocusListener = navigation.addListener('willFocus', () => {
BackHandler.addEventListener('backTapped', this.backButtonTap);
});
this.willBlurListener = navigation.addListener('willBlur', () => {
BackHandler.removeEventListener('backTapped', this.backButtonTap);
});
}
ponentWillUnmount() {
this.willFocusListener.remove();
this.willBlurListener.remove();
}
Then NavigationEvents ponent could be helpfull too
本文标签: javascriptWhere to call addListener in react nativeStack Overflow
版权声明:本文标题:javascript - Where to call addListener in react native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745639611a2667772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论