Resetting screens to default react-native navigation - react-native

I am using this library with bottom tabs:
https://github.com/wix/react-native-navigation
for navigation, I have 3 tabs at the bottom, one of them is for home, the thing is I move from home screen to another screen it gets added to the stack, I want to be able to reset the stack whenever I click to the home icon again at bottom tabs.
The route.js is sth like this for the home icon at bottomTab:
stack: {
children: [
{
component: {
name: home,
}
},
],
options: {
bottomTab: {
iconInsets: {top: 6, left: 0, bottom: -6, right: 0},
titleDisplayMode: 'alwaysHide',
icon: require('../assets/images/home.png'),
selectedIconColor: colors.primary,
}

First you have to add listener if user click the bottom tab . With help of registerbottomtabselectedlistener you can achieve this . You can use popToRoot to send user to root of stack
// Subscribe
const bottomTabEventListener = Navigation.events().registerBottomTabSelectedListener(({ selectedTabIndex, unselectedTabIndex }) => {
Navigation.popTo(componentId); // check selectedTabIndex and send to home
});
...
// Unsubscribe
bottomTabEventListener.remove();

Not sure from the snippet you posted but I think you are trying to get bottom tabs to work. This is incomplete but hopefully this gets you on track.
const AppTabs = createBottomTabNavigator({
App: AppStack, // stack navigator
Contacts: ContactsStack, // stack navigator
Settings: {
screen: SettingsScreen, // single component import
navigationOptions: {
tabBarLabel: 'Settings',
tabBarIcon: ({ focused }) => (
<Ionicons
size={26}
name={Platform.OS === 'ios' ? 'ios-settings' : 'settings'}
style={{ marginBottom: -3 }}
color={focused ? "red" : "gray"}
/>
),
}
},
}, {
initialRouteName: 'App',
tabBarPosition: 'bottom',
swipeEnabled: true,
tabBarOptions: {
activeTintColor: 'green',
inactiveTintColor: 'gray',
labelStyle: {
fontSize: 16,
},
tabStyle: { marginBottom: -10 }
}
});
export default createAppContainer(createSwitchNavigator({
AuthLoading: AuthLoadingScreen,
App: AppTabs,
Auth: AuthStack,
}, {
initialRouteName: 'AuthLoading',
}));

Related

Why do I get a blank screen after using Drawer-Navigator in React-Native to navigate to another Navigator?

Edit Snack: https://snack.expo.io/rJfyQLhJ8
I am learning react native using some course material which is pretty outdated. There have been some breaking changes and I am trying to figure things out. This is my Main Component with a few navigators.
MenuNavigator:
const MenuNavigator = createStackNavigator(
{
Menu: { screen: Menu },
Dishdetail: { screen: Dishdetail }
},
{
initialRouteName: 'Menu',
defaultNavigationOptions: {
flex: 1,
headerStyle: {
backgroundColor: '#512DA8',
},
headerTintColor: '#fff',
headerTitleStyle: {
color: '#fff'
}
}
})
Home Navigator
const HomeNavigator = createStackNavigator(
{
Home: { screen: Home }
},
{
defaultNavigationOptions: {
headerStyle: {
backgroundColor: '#512DA8',
},
headerTintColor: '#fff',
headerTitleStyle: {
color: '#fff'
}
}
})
And finally here is the MainNavigator
const MainNavigator = createDrawerNavigator(
{
Home:
{
screen: HomeNavigator,
navigationOptions: {
title: 'Home',
drawerLabel: 'Home'
}
},
Menu:
{
screen: MenuNavigator,
navigationOptions: {
title: 'Menu',
drawerLabel: 'Menu'
},
}
},
{
drawerBackgroundColor: '#D1C4E9'
});
After this, I wrap it in the createAppContainer and use it in the render method.
const App = createAppContainer(MainNavigator);
class Main extends Component {
render() {
return (
<View style={{ flex: 1, paddingTop: Platform.OS === 'ios' ? 0 : Expo.Constants.statusBarHeight }}>
<App />
</View>
);
}}
The app builds successfully and I am able to access the side drawer but when i click on the Menu item, it navigates without throwing an error but shows a blank white screen. If i navigate back to the home screen it will display it properly but it will show a blank screen in place of the Menu screen/component. Why is this ? I am using a OnePlus5t.
It might be a bug. It works after you navigate to the home screen using the android home UI button and back into the application. Seems to work fine from then onwards.

Pass to props to all screen of topnavigations

I'm trying to send props to all the screen children but only the default screen get the props.
I want to send the props to all the screen of the top navigation. I tried to navigate directly to the tab but it doesn't work either . I don't know what i'm doing wrong.
class RestaurantSummary extends Component {
render () {
return (
<TouchableRipple
onPress={() => this.props.nav.navigation.navigate("Plat", { restaurant: this.props.restaurant,
token: this.props.token, uri: uri})}>
<Card style={styles.container}>
<Card.Cover source={{ uri: uri }} />
<Card.Content style={styles.content}>
<Text style={styles.Text}>{this.props.restaurant.name} - {this.props.restaurant.address} </Text>
</Card.Content>
</Card>
</TouchableRipple>
)
}
}
Here is how i built my navigations . I have a switch navigator which allows me to switch between the app stack and the home stack. And in the App stack i have top bar navigations.
const TabScreen = createMaterialTopTabNavigator(
{
Entrées: { screen: Entrées },
Plat: { screen: Plat },
},
{
tabBarPosition: 'top',
swipeEnabled: true,
animationEnabled: true,
tabBarOptions: {
activeTintColor: '#FFFFFF',
inactiveTintColor: '#F8F8F8',
style: {
backgroundColor: '#633689',
},
labelStyle: {
textAlign: 'center',
},
indicatorStyle: {
borderBottomColor: '#87B56A',
borderBottomWidth: 2,
},
},
}, {
initialRouteName: 'Plat',
}
);
const top = createStackNavigator({
TabScreen: {
screen: TabScreen,
navigationOptions: {
headerStyle: {
backgroundColor: 'transparent',
},
headerTintColor: '#FFFFFF',
title: 'TabExample',
},
},
});
const AuthStack = createStackNavigator({
SignIn: {
screen: SignIn
},
SignUp: {
screen: SignUp
},
SignUpBiss: {
screen: SignUpBiss
}
},{
navigationOptions: {
headerTransparent: true,
},
mode: 'modal',
headerBackTitleVisible: 'true'
})
const AppStack = createStackNavigator({
Home: {
screen: Home
},
tab: top,
RestaurantDetail: {
screen: RestaurantDetail,
navigationOptions:()=>{
return {
tabBarVisible:false,
};
}
},
})
AppStack.navigationOptions = ({ navigation }) => {
let tabBarVisible = true;
if (navigation.state.index > 0) {
tabBarVisible = false; }
return {
tabBarVisible,
}
}
const bottomNav = createBottomTabNavigator({
Home : AppStack,
Logout : {screen: Logout}
})
const Nav = createSwitchNavigator({
App: bottomNav,
Auth: AuthStack
}, {
initialRouteName: 'Auth',
})
export default createAppContainer(Nav)
I saw your problem and I feels your code is little bit in a chaos.
firstly you can change
onPress={() => this.props.nav.navigation.navigate("Plat",
to
onPress={() => this.props.nav.navigation.navigate("TabScreen",
and then try this code in your Tab Screens
this.props.navigation.dangerouslyGetParent().getParam('restaurant')
this.props.navigation.dangerouslyGetParent().getParam('token')
this.props.navigation.dangerouslyGetParent().getParam('uri')
Hope it helps .feel free for doubts

How to make tabs inside stack in react native

I want to make a design like given in below image.
https://imgur.com/a/ehRthSS "image"
Here is my code for what is have tried uptil now
const NotificationTab = createMaterialTopTabNavigator({
NotificationsActivity: NotificationsActivity,
NotificationsList: NotificationsList,
},
{
initialRouteName: 'NotificationsActivity',
tabBarOptions: {
style: {
backgroundColor: 'red'
}
},
tabBarPosition: 'top',
})
I have two screens for the two tabs.
Here is my drawer navigator which calls that stack
const memberdrawerNavigator = createDrawerNavigator(
{
Notifications : { screen: NotificationTab},
ResetPassword : { screen: ResetPassword},
},
{
gesturesEnabled: true,
contentComponent: props => <DrawerMember {...props} />
},
{
contentOptions: {
}
}
);
Here is the main container for the main naviagator of the application
const AppNavigator = createAppContainer(RootStack)
export default AppNavigator;
Please help me how can i make the design given in the image? Just give me blueprint for the desing and i will make it.
Thanks in advance
You can make the MaterialTopTabNavigator as follows by adding some props to it.
const NotificationTab = createMaterialTopTabNavigator({
NotificationsActivity: NotificationsActivity,
NotificationsList: NotificationsList,
},
{
initialRouteName: 'NotificationsActivity',
tabBarOptions: {
//use this for change the color of active tab's label.
activeTintColor: "red",
//use this for change the color of inactive tabs' label.
inactiveTintColor: "black",
//use this for change the label styles.
labelStyle: {
fontSize: 30,
},
//use this for change the color of inactive tab.
inactiveBackgroundColor: "white",
//use this for change the style of vanigation bar.
style: {
backgroundColor: 'white',
},
//use this for change the style of indicator.
indicatorStyle: {
backgroundColor: 'white'
}
},
tabBarPosition: 'top',
})
for more info, https://reactnavigation.org/docs/en/material-top-tab-navigator.html

How to use custom width for each tabs in BottomTabBar react navigation?

I'm using react navigation V3 and I have a bottom tab navigation in my app
as follow:
export default createBottomTabNavigator({
profile: {
screen: ProfileUser,
navigationOptions:{
title : 'Profile',
}
},
leaderboard :{
screen : Leaderboard,
navigationOptions:{
title : 'Leaderboard',
}
},
home :{
screen : Homeboard,
navigationOptions:{
title : 'Home',
tabBarIcon: ({tintColor}) => (
<BattleTabIcon
width={30}
height={30}
/>
),
},
},
store :{
screen : AppStore,
navigationOptions:{
title : 'Store',
}
},
setting :{
screen : Settings,
navigationOptions:{
title : 'Setting',
}
},
},{
tabBarOptions: {
scrollEnabled : true,
upperCaseLabel: false,
activeTintColor: '#fff',
activeBackgroundColor :'#1f1f3c',
inactiveTintColor: '#8583a9',
showLabel: false,
style: {
backgroundColor: '#131332',
height: 50,
borderTopWidth: 2,
borderTopColor: '#aeaeae',
},
labelStyle: {
fontSize: 12,
fontFamily : 'Questrial-Regular',
marginBottom: 5
},
},
initialRouteName : 'home',
});
I have 5 tabs, I want to have more width for my center tab, also I need to have border for each on but I dont know how to do that.
please help me with your advices.
Thanks.
You could always create a custom tab component and do it yourself:
const MainTabNavigator = TabNavigator({
Home: {
screen: HomeScreen,
},
Tab2: {
screen: EmptyTab,
},
Tab3: {
screen: EmptyTab,
}
}, {
initialRouteName: 'Home',
tabBarPosition: "bottom",
tabBarComponent: props => {
return <TabNavigation {...props}
items={[
{
text: "Home", icon: { name: "home", type: "Entypo" },
route: "Home"
},
{
text: "Tab2", icon: { name: "data-usage", type: "MaterialIcons" },
route: "Tab2",
expand: true
},
{
text: "Tab3", icon: { name: "package", type: "Octicons" },
route: "Tab3"
}
]}
/>
}
});
And then use the expand prop of that item to control the style. This is my TabNavigation:
class TabNavigation extends React.PureComponent {
route = (route) => {
this.props.navigation.navigate(route);
}
render(){
let tabs = this.props.items.map((item, index) => {
let active = ((this.props.navigationState && this.props.navigationState.index === index) || (this.props.navigation && this.props.navigation.state && this.props.navigation.state.index === index));
let icon, text = null;
if (item.icon){
let iconStyle = {
color: active ? this.props.theme.navBarTextColorActive : this.props.theme.navBarTextColor,
size: 23
};
if (item.icon.size)
iconStyle.fontSize = item.icon.size;
if (item.icon.color)
iconStyle.color = item.icon.color;
icon = this.props.getIcon(item.icon.type, item.icon.name, iconStyle.size, iconStyle.color);
}
if (item.text)
text = <Text active={active}>{item.text}</Text>;
return (<TouchableOpacity key={index} onPress={() => this.route(item.route)} style={{flex: 1}}>
<View style={styles.buttonWrapper}>
{icon}
{text}
</View>
</TouchableOpacity>)
});
if (tabs.length == 0)
return null;
else
return (<NavBar>{tabs}</NavBar>)
}
}
You will need to add the logic there for the styles.

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,
}
});