Without using Redux, how do I detect a tab change with a react navigation tab navigator?
I know I need to somehow use onNavigationStateChange but I can\'t figure out
If you are using react-navigation version 5, then you can add tab-press
listener right in your tab screen definition, to do some pre-processing, as mentioned in docs
{
if (userIsNotLoggedIn) {
// Prevent default action
e.preventDefault();
navigation.navigate("LoginScreen");
}
},
}}
/>
Explanation: When Home
tab will be clicked in BottomBar
, and if user is not logged in, the HomeScreen
won't open and instead LoginScreen
will open(Note: LoginScreen
is navigation name which will be registered some where with a screen). But if user is logged in, then it will behave normally.