React Native Conditional TabNavigator - react-native

I Have the following structure of Navigation. On my Third tab, app has to load based the user login status i.e., if user logged in before load UserProfileScreen or load CreateAnAccountScreen. Now, user has not logged in and app has loaded the third tab with CreateAnAccountScreen, when user tries to login, upon successful, the app has to navigate to UserProfileScreen under the same tab.
As am new to react native I am not sure how this navigation system works.
const Stylelist = StackNavigator({
LoginScreen: {
screen: LoginScreen,
navigationOptions: ({ navigation }) => ({
header: null,
}),
},
SplashScreen: {
screen: SplashScreen,
navigationOptions: ({ navigation }) => ({
header: null,
}),
},
LaunchScreen: {
screen: LaunchScreen,
navigationOptions: ({ navigation }) => ({
header: null,
}),
},
Home: {
screen: TabNavigator({
RemoteControlScreen: {screen: RemoteControlScreen, navigationOptions: ({ navigation }) => ({header: null, tabBarIcon: ({focused }) => <Image source={Images.iconDrop} style={{tintColor: focused ? Colors.lightGreen : null}}/>}),},
GetInspiredScreen: {screen: GetInspiredScreen, navigationOptions: ({ navigation }) => ({header: null, tabBarIcon: ({focused }) => <Image source={Images.iconGetInspired} style={{tintColor: focused ? Colors.lightGreen : null}}/>}),},
...AppConfig.isUserRegistered ?
{ UserProfileScreen: {screen: UserProfileScreen, navigationOptions: ({ navigation }) => ({header: null, tabBarIcon: ({focused }) => <Image source={Images.iconProfile} style={{tintColor: focused ? Colors.lightGreen : null}}/>}),}, } :
{
CreateAnAccountScreen: {screen: CreateAnAccountScreen, navigationOptions: ({ navigation }) => ({header: null, tabBarIcon: ({focused }) => <Image source={Images.iconProfile} style={{tintColor: focused ? Colors.lightGreen : null}}/>}),},
}
},
{
tabBarPosition: 'bottom',
animationEnabled: false,
swipeEnabled: false,
showLabel: false,
tabBarOptions: {
activeTintColor: 'yellow',
inactiveTintColor: 'black',
showLabel: false,
showIcon: true,
style: {
height: (deviceHeight * 5) / 67,
justifyContent: 'center',
backgroundColor: Colors.background,
},
}
}),
navigationOptions: ({ navigation }) => ({
// title: 'Home',
}),
},
},
{
headerMode: 'none',
initialRouteName: 'SplashScreen',
navigationOptions: {
headerStyle: styles.header
}
});
export default Stylelist;

Related

React-navigation open screen out of bottomNavigation

I have a bottomTabNavigator with some groups of screens:
{
Home: {
screen: Home,
navigationOptions: {
tabBarTestID: 'homeMenuButton',
tabBarIcon: TabItem('home'),
},
},
Beneficios: {
screen: AdvantageClub,
navigationOptions: {
tabBarTestID: 'beneficiosMenuButton',
tabBarIcon: TabItem('loyalty'),
tabBarLabel: 'BenefĂ­cios',
},
},
Repom: {
screen: RepomScreen,
navigationOptions: {
tabBarTestID: 'saldoMenuButton',
tabBarIcon: TabItem('attach-money'),
tabBarLabel: 'Saldo',
},
},
Profile: {
screen: Profile,
navigationOptions: {
tabBarTestID: 'profileMenuButton',
tabBarIcon: TabItem('person'),
tabBarLabel: 'Perfil',
},
},
},
{
initialRouteName: 'Home',
tabBarOptions: {
activeTintColor: colors.blue.primary,
inactiveTintColor: colors.blue.secondary,
style: {
height: 64,
borderTopWidth: 0,
paddingVertical: 0,
},
tabStyle: {
paddingVertical: 8,
},
},
},
);
Inside of "Beneficios" (screen: AdvantageClub) i have one StackNavigator:
const AdvantageClub = createStackNavigator(
{
WebView: AdvantageClubScreen,
AdvantageClubSignUp: AdvantageClubSignUpScreen,
AdvantageClubSignUpConfirm: AdvantageClubSignUpConfirmScreen,
AdvantageClubTerms: AdvantageClubTermsScreen,
},
{
initialRouteName: 'AdvantageClubSignUp',
...noHeader,
},
);
I need to open this WebView (AdvantageClubScreen) out of my bottomTabNavigator, like a new window. When i use navigator.navigate('WebView') the bottom menu keeps on screen.
What version of react-navigation are you using ?
I guess it's the V4, then in your nested StackNavigator "AdvantageClub" you can use tabBarVisible property in navigationOptions with something like this :
AdvantageClub.navigationOptions = ({navigation}) => {
const routes = navigation.state.routes;
const tabBarVisible = routes[routes.length -1].routeName === 'WebView'
return {tabBarVisible};
};

ReactNative Combining StackNavigation with DrawerNavigation

I am using both createStackNavigator & createDrawerNavigator but the stackNavigator isn't working i can't navigate to any page. here is the app.js
const DrawerNavigator = createDrawerNavigator({
homeScreen: {
screen: homeScreen,
navigationOptions: ({ navigation }) => {
return {
title: 'Home',
headerStyle: {
backgroundColor: '#231f20',
height: 0,
},
headerLeft: null,
gesturesEnabled: false,
}
},
},
categoryScreen: {
screen: categoryScreen,
navigationOptions: ({ navigation }) => ({
title: 'Makeup',
//title: navigation.getParam('category'),
headerStyle: {
backgroundColor: '#231f20',
},
headerTintColor: '#f5f5f5',
}),
},
categoryScreen: {
screen: categoryScreen,
navigationOptions: ({ navigation }) => ({
title: 'Photographers',
//title: navigation.getParam('category'),
headerStyle: {
backgroundColor: '#231f20',
},
headerTintColor: '#f5f5f5',
}),
},
},
{
contentOptions: {
activeTintColor: '#d03036',
activeBackgroundColor : '#f5f5f5',
inactiveBackgroundColor : 'rgba(0,0,0,0)',
inactiveTintColor: '#545f7a',
itemStyle: {
marginVertical: 0,
},
labelStyle: {
fontWeight:'bold',
backgroundColor: 'transparent'
}
},
headerMode: 'screen', // screen on android
// headerBackTitleVisible: false,
headerTransitionPreset: 'fade-in-place',
headerLayoutPreset: 'center',
cardStyle: {},
initialRouteName: 'homeScreen',
drawerPosition: 'left',
contentComponent: CustomDrawerContentComponent,
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle'
});
const AuthStackNavigator = createStackNavigator(
{
loadingScreen: {
screen: loadingScreen,
navigationOptions: ({ navigation }) => {
return {
header: null,
}
},
},
loginScreen: {
screen: loginScreen,
navigationOptions: ({ navigation }) => {
return {
header: null,
}
},
},
DrawerNavigator: {
screen: DrawerNavigator,
navigationOptions: ({ navigation }) => {
return {
header: null,
gesturesEnabled: false
}
},
},
},
{
initialRouteName: 'loadingScreen',
// mode: 'modal',
headerMode: 'screen', // screen on android
// headerBackTitleVisible: false,
headerTransitionPreset: 'fade-in-place',
headerLayoutPreset: 'center',
cardStyle: {},
}
);
const NavigationApp = createAppContainer(AuthStackNavigator);
// Export the App
export default NavigationApp;
import { useScreens } from 'react-native-screens';
useScreens();
i tried exporting the DrawerNavigator but then the stacknavigator isn't working anymore can i use both at the same time ?
also i got design issue when doing so i tried searching for hours but with no luck

react navigation back button back to spalsh screen

I want to use react navigation in react native project but when I use this code in header it shows some back button in every screen and when I click on this it goes back to the splash screen. How to use react navigation with check first splash and then go to stack home and show back button in any screen.its very confuse me.
const TabNav=createBottomTabNavigator({
Notification:{
screen:Notif,
navigationOptions: () => ({
title:'',
tabBarIcon: ({ tintColor }) => {
return (
<IconIonicons
name='ios-notifications'
size={40}
color={tintColor}
/>
);
},
tabBarOptions: {
activeTintColor: '#000',
}
})
},
Home:{
screen:Home3,
navigationOptions: () => ({
title:'',
tabBarIcon: ({ tintColor }) => {
return (
<Image style={{ width: 40, height: 40,marginTop:'20%' }} source={Home}/>
);
}
})
},
User:{
screen:ProfileScreen,
navigationOptions: () => ({
title:'',
tabBarIcon: ({ tintColor }) => {
return (
<IconIonicons
name='ios-person'
size={40}
color={tintColor}
/>
);
},
tabBarOptions: {
activeTintColor: '#000',
}
})
},
},
{
initialRouteName:"Home"
},{
header: null,
headerMode: 'none',
}
)
const StackHome = createStackNavigator({
Tabs:{
screen:TabNav
},
CardView:{
screen:CardView,
},
Thumb:{
screen:Thumb,
}, Item:{
screen:Item,
}, Product:{
screen:ProductScreen,
}, Festivals:{
screen:Festivals,
} , Review:{
screen:Review,
} , Movie:{
screen:Movie,
} , Naghd:{
screen:Naghd,
} , Advertisment:{
screen:Advertisment,
} , Advertis:{
screen:Advertis,
}, CreateAd:{
screen:CreateAd,
}, Insta:{
screen:Insta,
}, Test:{
screen:Test,
}, ForoshRah:{
screen:ForoshRah,
}, Home2:{
screen:Home2,
}, Login:{
screen:Login,
}, Elan:{
screen:Elan,
}, Sabtenam:{
screen:Sabtenam,
}, sponser:{
screen:sponsor,
},Splash:{
screen:Spalsh
},Products:{
screen:Products
},
initialRouteName : 'Home',
},{
headerMode:'none',
navigationOptions: {
header: (props) => <ImageHeader {...props} />,
}
}
)
const RootNav = createStackNavigator({
StackHome:{
screen:StackHome
},
Splash:{
screen:Spalsh
},Login:{
screen:Login
},
},{
initialRouteName : 'Splash',
header: null,
});
You can use SwitchNavigator of React Navigation. As the SwitchNavigator is to only ever show one screen at a time. By default, it does not handle back actions and it resets routes to their default state when you switch away.
Kindly refer this https://reactnavigation.org/docs/en/switch-navigator.html
Remove the **Splash** Screen from your **StackHome** StackNavigator
Alter your **RootNav** with Switch Navigator like below
// Uses SwitchNavigator
const RootNav = createSwitchNavigator({
StackHome:{
screen: StackHome
},
Splash:{
screen: Splash
},Login:{
screen: Login
},
},{
initialRouteName : 'Splash'
});
Your StackHome consist of some stack navigators screens, you can set the navigation options from there itself to set the image in the header. You can set like below.
const StackHome = createStackNavigator({
CardView:{
screen: CardView,
navigationOptions: ({ navigation }) => ({
headerTitleStyle: { flex: 1, fontWeight: 'normal', fontSize: 15, color: '#FFFFFF', fontFamily: 'DroidSans-Bold' },
headerTintColor: "#2662b2",
headerStyle: {
backgroundColor: "#05BFFF",
},
headerRight:(<View><NotificationComponent navigation={navigation}/></View>)
})
},
Thumb:{
screen: Thumb,
}, Item:{
screen: Item,
}
});

How to pass props to a function in BottomTabNavigator

I have searched quite a bit in SO and haven't found a working solution to my problem,
Problem Statement : I have BottomTabNavigator with 5 tabs. Everytime i switch the tab i need to make an api call to fetch latest information. And to fetch latest information i need to have the redux store values that i need to use to make the api call. So how do i make those redux store values available in the function call that i make on tabChange?
Below is my function which needs to be called followed by the BottomTabNavigator,
fetchProfile = () => {
const {selectedPerson, dispatch, actions} = this.props
let Id = selectedPerson.Id
let personId = selectedPerson.placeId
dispatch(actions.getPersonDetails(Id, personId))
}
export const Tabs = createBottomTabNavigator({
Profile: {
screen: ProfileConnector,
navigationOptions: {
tabBarLabel: 'Profile',
tabBarIcon: ({tintColor}) => <Icon name="user" size={px2dp(22)} color={tintColor} onPress={() => fetchProfile()}/>,
},
// navigationOptions: ({ navigation }) => ({
// tabBarOnPress: (tab, jumpToIndex) => {
// console.log('onPress:', tab.route);
// jumpToIndex(tab.index);
// },
// tabBarLabel: 'Profile',
// tabBarIcon: ({tintColor}) => <Icon name="user" size={px2dp(22)} color={tintColor}/>,
// }),
},
Inbox: {
screen: InboxConnector,
navigationOptions: {
tabBarLabel: 'Inbox',
tabBarIcon: ({tintColor}) => <Icon name="inbox" size={px2dp(22)} color={tintColor}/>,
},
},
Bling: {
screen: BlingConnector,
navigationOptions: {
tabBarLabel: 'Bling',
tabBarIcon: ({tintColor}) => <Icon name="hand" size={px2dp(22)} color={tintColor}/>,
},
},
Calendar: {
screen: CalendarConnector,
navigationOptions: {
tabBarLabel: 'Calendar',
tabBarIcon: ({tintColor}) => <Icon name="calendar" size={px2dp(22)} color={tintColor}/>,
},
},
Misc: {
screen: Misc,
navigationOptions: {
tabBarLabel: 'Misc',
tabBarIcon: ({tintColor}) => <Icon name="bar-graph" size={px2dp(22)} color={tintColor}/>,
},
},
}, {
initialRouteName: 'Inbox',
tabBarPosition: 'bottom',
swipeEnabled: true,
scrollEnabled: true,
tabBarOptions: {
activeTintColor: 'teal',
inactiveTintColor: '#424949',
activeBackgroundColor: "white",
inactiveTintColor: '#424949',
labelStyle: { fontSize: 14 },
style : { height : 50},
}
});
Any help is much appreciated, i'm loosing my mind over this.
I have tried the commented out section above and with that it throws jumpToIndex is not a function
and the other option doesn't have the redux store values.
Please help. :(
Thanks,
Vikram
"Everytime i switch the tab i need to make an api call to fetch latest information."
You can detect willFocus/didFocus event of react-navigation and then fetch api
react-navigation: Detect when screen is activated / appear / focus / blur

React-Navigation Drawer and Static Tab-Navigation

i am switching from react navigator to react-navigation and i am actually fine with this approach, but struggeling with one issue.
I want to use a drawer navigation and a bottom aligned Tab-Navigation.
This part is working as expected - no issues here.
I want to have the tabbed navigation fixed with 3 Buttons that will have the same action all over the app. ( eg. dashboard / search / favourites )
From Dashboard you can navigate one level deeper. As i am doing it now, the Label of the Tab formerly "Dashboard" changes to the Name of the navigated-to Item.
to clarify, i added a stack-navigation in the Dashboard-Screen-Tab, so the user can navigate through that pages.
How can i prevent the tabs' labes and actions to change while navigating within the tabs' stack?
Basically i want a fixed Tab Navigation on each screen.
Should i create a fixed View-Component to achieve that?
Here is my setup:
App.js
const MetaTabNavigator = TabNavigator({
Dashboard: {
screen: MachineNavigator
},
Search: { screen: SearchScreen },
Favourites: { screen: FavouritesScreen },
},
{
tabBarPosition: Platform.OS === "ios" ? 'bottom' : 'top',
animationEnabled: true,
tabBarOptions: {
activeTintColor: STYLES.HIGHLIGHT_COLOR
},
swipeEnabled: false,
backBehavior: 'none'
});
const MetaDrawerNavigator = DrawerNavigator({
Home: {
screen: MetaTabNavigator,
navigationOptions: {
drawer: {
label: 'Drawer',
icon: ({ tintColor }) => <Icon name="rocket" size={24} />
},
},
}
},
{
contentComponent: props => <Menu {...props} />
}
);
AppRegistry.registerComponent('myApp', () => MetaDrawerNavigator);
MachineNavigator
const MachineNavigator = StackNavigator({
Main: {
screen: MachineOverview,
navigationOptions: ({ navigation }) => ({
title: "Dashboard",
headerLeft: (
<TouchableOpacity onPress={() => navigation.navigate("DrawerOpen")}>
<IOSIcon name="ios-menu" size={30} />
</TouchableOpacity>
),
headerStyle: { paddingRight: 10, paddingLeft: 10 }
})
},
Category: {
screen: Category,
navigationOptions: (props) => ({
title: "Kategorie",
})
},
ItemDetail: {
screen: ItemDetail,
navigationOptions: (props) => ({
title: "Video",
})
}
})
export default MachineNavigator;
According to this issue, you can add to the tab configuration the tabBarLabel property to control the label:
const MetaTabNavigator = TabNavigator({
Dashboard: {
screen: MachineNavigator, navigationOptions: {tabBarLabel: 'Dashboard'}
},
...