I have an top level SwitchNavigator with two stacks (AuthStack and MainStack)
MainStack contains the stack of an logged user.
When I try to switch from one screen to other it's fine, but if I start an action that change the state of the current screen, if I leave and then come back the state keeps the same, it's like the screens was not unmounted when I switch between others.
Working example: https://snack.expo.io/HJrslFk34
Try to move to settings, click on "add" and then switch between the screens, the state keeps the same and screens were not being unmounted
const MainStack = createBottomTabNavigator(
{
[homeDrawerLabel]: {
screen: HomeNavigator,
navigationOptions: {
tabBarLabel: homeDrawerLabel,
tabBarIcon: ({ tintColor }) => (
<Ionicons
name="ios-bookmarks"
size={25}
color={tintColor}
/>
),
},
},
[calendarDrawerLabel]: {
screen: CalendarNavigator,
navigationOptions: {
tabBarLabel: calendarDrawerLabel,
tabBarIcon: ({ tintColor }) => (
<Ionicons
name="ios-bookmarks"
size={25}
color={tintColor}
/>
),
headerLayoutPreset: "center",
},
},
[messagesDrawerLabel]: {
screen: MessagesNavigator,
navigationOptions: {
tabBarLabel: messagesDrawerLabel,
tabBarIcon: ({ tintColor }) => (
<Ionicons
name="ios-bookmarks"
size={25}
color={tintColor}
/>
),
},
},
[notificationsDrawerLabel]: {
screen: NotificationsNavigator,
navigationOptions: {
tabBarLabel: notificationsDrawerLabel,
tabBarIcon: ({ tintColor }) => (
<IconNavigatorWithBadge
badgeCount={3}
name="md-checkmark-circle"
size={25}
color={tintColor}
/>
),
},
},
EditProfile: { screen: EditProfile },
[paymentDrawerLabel]: {
screen: PaymentsNavigator,
navigationOptions: {
tabBarLabel: paymentDrawerLabel,
tabBarIcon: ({ tintColor }) => (
<Ionicons
name="ios-bookmarks"
size={25}
color={tintColor}
/>
),
},
},
},
{
tabBarOptions: {
activeTintColor: "#29C2AF",
inactiveTintColor: "rgba(41, 194, 175, 0.4)",
style: {
height: 60,
paddingVertical: 10,
borderTopColor: "rgba(0,0,0,0.2)",
shadowColor: "#000",
shadowOffset: {
width: 2,
height: 5,
},
shadowOpacity: 0.75,
shadowRadius: 3.84,
elevation: 7,
},
},
headerMode: "none",
headerLayoutPreset: "center",
},
);
const AppNavigator = createSwitchNavigator(
{
AuthStack: AuthStack,
Main: MainStack,
},
{
headerMode: "none",
initialRouteName: "AuthStack",
}
);
const AppContainer = createAppContainer(AppNavigator);
With React Navigation, Tab Navigator views are not unmounted when switching between tabs. You can see the details about the lifecycle in react-navigation in their documentation here: Navigation lifecycle - Example scenario, the important part being here:
We start on the HomeScreen and navigate to DetailsScreen. Then we use the tab bar to switch to the SettingsScreen and navigate to ProfileScreen. After this sequence of operations is done, all 4 of the screens are mounted!
If you want to know and act on the active tab, what you will be looking for are the focus/blur events outlined here
Related
I'm using a tab bottom navigator in my project. I want to show a new page in a tab of the tab bottom navigator but I want the tab navigator to appear again at the bottom. I don't want to add the button of the new page I want to show below. How do I manage this situation.I created the new scene as another js file and added it to the app js. I mean. I have a button to redirect to a new page in the Home tab, but how do I navigate
my navigator codes in app.js:
import LoadingScreen from './screens/LoadingScreen'
import HomeScreen from './screens/HomeScreen'
import LoginScreen from './screens/LoginScreen'
import RegisterScreen from './screens/RegisterScreen'
import ProfileScreen from './screens/profileScreen'
import NotificationScreen from './screens/notificationScreen'
import PostScreen from './screens/postScreen'
import soruOnaylaScreen from './screens/soruOnaylaScreen'
const AppContainer = createStackNavigator(
{
default: createBottomTabNavigator(
{
soruOnayla: {
screen: soruOnaylaScreen,
navigationOptions: {
tabBarIcon: ({ tintColor }) =>
<Ionicons name="ios-chatboxes" size={24} color={tintColor} />
}
},
Home: {
screen: HomeScreen,
navigationOptions: {
tabBarIcon: ({ tintColor }) =>
<Ionicons name="ios-home" size={24} color={tintColor} />
}
},
Post: {
screen: PostScreen,
navigationOptions: {
tabBarIcon: ({ tintColor }) =>
<Ionicons
name="ios-add-circle"
size={36}
color='#E9446A'
style={{
shadowColor: "#E9446A",
shadowOffset: { width: 0, height: 0 },
shadowRadius: 10,
shadowOpacity: 0.3
}}
/>
}
}
, Notification: {
screen: NotificationScreen,
navigationOptions: {
tabBarIcon: ({ tintColor }) =>
<Ionicons name="ios-notifications" size={24} color={tintColor} />
}
},
Profile: {
screen: ProfileScreen,
navigationOptions: {
tabBarIcon: ({ tintColor }) =>
<Ionicons name="ios-person" size={24} color={tintColor} />
}
}
},
{
tabBarOptions: {
activeTintColor: "#161f3d",
inactiveTintColor: "#b8bbc4",
}
}
),
postModal: {
screen: PostScreen
}
},
{
mode: "modal",
headerMode: "none"
}
)
const AuthStack = createStackNavigator({
Login: LoginScreen,
Register: RegisterScreen
})
export default createAppContainer(
createSwitchNavigator(
{
Loading: LoadingScreen,
App: AppContainer,
Auth: AuthStack
},
{
initialRouteName: "Loading"
}
)
)
I can't show my custom header with static navigation options. It's only TabNavigator on my Title. What should I do?
This is my HomeScreen Component;
export default class Home extends Component {
static navigationOptions = ({navigation}) => ({
title: 'MENEMENOYS',
headerLeft : (
<TouchableOpacity
onPress={() => {
AuthStore.LogOut().then(() => {
navigation.navigate('LoginPage');
});
}}>
<Icon
name="md-log-out"
size={30}
color={'white'}
style={{marginLeft:10}}
/>
</TouchableOpacity>
)
})
And this is my router : stack navigator with tab navigator.
const TabNavigator = createBottomTabNavigator(
{
Home: {
screen: Home,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon
name="md-home"
size={30}
color={tintColor}
style={styles.homeIcon}
/>
),
},
},
AddNewCar: {
screen: AddNewCar,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon
name="md-add-circle"
size={40}
color={tintColor}
style={styles.addIcon}
/>
),
},
},
OtoparkList: {
screen: OtoparkList,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<IconAwesome name="car" size={30} color={tintColor} />
),
},
},
},
{
tabBarOptions: {
showLabel: false,
activeTintColor: '#41AB5F',
inactiveTintColor: 'white',
style: {
backgroundColor: '#1F202D',
borderTopColor: 'transparent',
},
},
},
);
const AppNavigator = createStackNavigator(
{
TabNavigator: {
screen: TabNavigator,
navigationOptions: {
}
},
LoginPage: {
screen: LoginPage,
navigationOptions: {
headerShown: false,
},
},
ParkingDetail: {
screen: ParkingDetail,
navigationOptions: {
headerShown: false,
},
},
Print: {
screen: Print,
navigationOptions: {
headerShown: false,
}
}
},
{
initialRouteName: 'LoginPage',
},
);
But my header is not shown. Only Header title:TabNavigator and default back button.
Where am i doing wrong ? Please help me. Thanks.
Hey in React navigation latest version 5.x syntax is changed try following
Take a look at doc here Header buttons
static navigationOptions = ({ navigation }) => {
return {
headerTitle: () =>(
<View>
<Text>MENEMENOYS</Text>
</View>
),
headerLeft: () => (
<TouchableOpacity
onPress={() => {
AuthStore.LogOut().then(() => {
navigation.navigate('LoginPage');
});
}}>
<Icon
name="md-log-out"
size={30}
color={'white'}
style={{marginLeft:10}}
/>
</TouchableOpacity>
),
};
};
im using the Material-Bottom-Tab-Navigatior and would like to get a transparent bar background however my result atm looks like this
This is the code
import React from 'react';
import { createMaterialBottomTabNavigator } from 'react-navigation-material-bottom-tabs';
import Icon from 'react-native-vector-icons/Feather';
import HomePage from '../components/whatsOn/HomePage';
import ComingSoon from '../components/comingSoon/ComingSoon';
import Favourites from '../components/favourites/Favourites';
import config from '../static/config.json';
import texts from '../static/texts.json';
const { gradient } = config.colors;
const { ComingSoonTitle, HomeTitle, FavouritesTitle } = texts.Tabs;
const lang = config.language;
export const TabStack = createMaterialBottomTabNavigator({
comingSoon: {
screen: ComingSoon,
activeTintColor: gradient.primary,
navigationOptions: {
tabBarColor: 'transparent',
tabBarLabel: ComingSoonTitle[lang],
tabBarIcon: ({ tintColor }) => (<Icon name="calendar" color={tintColor} size={24} />),
},
},
whatsOn: {
screen: HomePage,
activeTintColor: gradient.primary,
navigationOptions: {
tabBarColor: 'transparent',
tabBarLabel: HomeTitle[lang],
tabBarIcon: ({ tintColor }) => (<Icon name="film" color={tintColor} size={24} />),
},
},
favourites: {
screen: Favourites,
activeTintColor: gradient.primary,
navigationOptions: {
tabBarColor: 'transparent',
tabBarLabel: FavouritesTitle[lang],
tabBarIcon: ({ tintColor }) => (<Icon name="star" color={tintColor} size={24} />),
},
},
}, {
shifting: true,
initialRouteName: 'whatsOn',
order: ['comingSoon', 'whatsOn', 'favourites'],
tabBarPosition: 'bottom',
});
this TabStack is nested inside this stack:
export const HomeStack = createStackNavigator({
Tabs: {
screen: TabStack,
navigationOptions: {
title: 'Compeso',
header: props => <CustomHeader {...props} />,
headerStyle: {
backgroundColor: 'transparent',
},
headerTitleStyle: {
fontSize: 24,
fontWeight: '500',
color: colors.typography,
},
headerRight: MenuIcon,
headerLeft: SearchIcon,
animationEnabled: true,
},
},
Drawer: {
screen: MoreInfromation,
},
}, {
initialRouteName: 'Tabs',
cardStyle: { backgroundColor: 'transparent' },
});
the HomeStack is nested in a switchNavigator
export default class Router extends Component {
render() {
return (
<MainStack />
);
}
}
const MainStack = createSwitchNavigator({
Auth: LoginStack,
Home: HomeStack,
},
{
initialRouteName: 'Home',
});
This MainStack is getting rendered in my App.js
I put this tab navigator into a stack navigator and each component that gets displayed shows a scroll view with a height of 815 pixels to make sure the background is behind the bar.
Can someone advice ?
Thanks in advance
You need to include barStyle.
So, for transparent the style will be something like this:
barStyle: {backgroundColor:'transparent'}
In your code it will look something like this(included at bottom).
export const TabStack = createMaterialBottomTabNavigator({
comingSoon: {
...
},
...
}, {
shifting: true,
initialRouteName: 'whatsOn',
order: ['comingSoon', 'whatsOn', 'favourites'],
tabBarPosition: 'bottom',
barStyle: {backgroundColor:'transparent'}
});
I want to create a drawer. For each screen accessed through the drawer I want to show the same header I am actually customizing on WelcomeContainer AppNavigator. The drawer should overlap the header. The problem is that the header appear only on the home screen: If I click on the the category menu, the drawer disappear. I even tried to copy the header code and past it in the category screen and nothing appeared. This is my code
const AppNavigator = StackNavigator({
Home: {
screen: WelcomeContainer,
navigationOptions: ({navigation}) => ({
headerLeft:
<Icon name="menu" color='#5c72b0' size={35} style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems:'center',
paddingLeft:10 }} onPress={ () => navigation.navigate('DrawerOpen') } />,
headerRight:
<Icon name="settings" color='#5c72b0' size={25} style={{ alignItems:'center', paddingRight:10 }}
onPress={ () => navigation.navigate('Settings', {title: I18n.t('settings.title') }) } /> })
},
Settings: {
screen: SettingsContainer,
navigationOptions: ({navigation}) => ({
title: navigation.state.params.title
})
},
About: {
screen: About,
navigationOptions: ({navigation}) => ({
title: navigation.state.params.title
})
}
})
const AppDrawer = DrawerNavigator(
{
Home: {
path: '/',
screen: AppNavigator,
},
Category: {
path: '/sent',
screen: CategoryContainer,
},
},
{
initialRouteName: 'Home',
contentOptions: {
activeTintColor: '#e91e63',
},
}
);
Could you please help me solve the problem?
I do like this https://github.com/react-community/react-navigation/issues/801
but it's can't work , it's always on top (Android)
How i can do to move TabBar to bottom of screen
export const Tabs = TabNavigator({
Feed: {
screen: FeedStack,
navigationOptions: {
header: null,
tabBarLabel: 'Module',
tabBarIcon: ({ tintColor }) => <Icon namax="list" size={35} color={tintColor} />,
},
},
Min: {
screen: Min,
navigationOptions: {
header: null,
tabBarLabel: 'Min',
tabBarIcon: ({ tintColor }) => <Icon namax="list" size={35} color={tintColor} />,
},
},
max: {
screen: max,
navigationOptions: {
header: null,
tabBarLabel: 'max',
tabBarIcon: ({ tintColor }) => <Icon namax="account-circle" size={35} color={tintColor} />
},
},
});
The general Api for TabNavigator is TabNavigator(RouteConfigs, TabNavigatorConfig). TabNavigatorConfig has various options one of them is tabBarPosition which takes 'top' or 'bottom'. Default is 'top' for android so change it to 'bottom' as shown below.
Check React-Navigation Tab docs
export const Tabs = TabNavigator(
{
Feed: {
screen: FeedStack,
navigationOptions: {
header: null,
tabBarLabel: 'Module',
tabBarIcon: ({ tintColor }) => <Icon namax="list" size={35} color={tintColor} />,
},
},
Min: {
screen: Min,
navigationOptions: {
header: null,
tabBarLabel: 'Min',
tabBarIcon: ({ tintColor }) => <Icon namax="list" size={35} color={tintColor} />,
},
},
max: {
screen: max,
navigationOptions: {
header: null,
tabBarLabel: 'max',
tabBarIcon: ({ tintColor }) => <Icon namax="account-circle" size={35} color={tintColor} />
},
}
},
{
tabBarPosition: 'bottom'
}
);