I couldn't use react-navigation tab bar. How to use this? - react-native

Now I am trying to create React Native app on Expo and use React-Navigation Tab Bar but I could't.
Actually I don't get any error but this code below doesn't work.
No warning as well.
import { createBottomTabNavigator, createAppContainer } from 'react-
navigation';
import Icon from 'react-native-vector-icons/FontAwesome';
import Home from './src/Screens/Home';
import Help from './src/Screens/Help';
const App = createBottomTabNavigator(
{
Home: {
screen: Home,
defaultNavigationOptions: {
tabBarIcon: ({ tintColor }) => {
<Icon name="home" style={{ width: 25, height: 25, tintColor:
tintColor}}/>
},
title: 'Home',
},
},
Help: { screen: Help },
},
{
swipeEnabled: false, //Android用
tabBarOptions: {
activeTintColor: '#DE628D',
inactiveTintColor: '#707070',
},
},
);
export default createAppContainer(App);

the tab works fine, but if you meant, there's no icon, try this instead
navigationOptions: {
tabBarIcon: ({ tintColor, activeTintColor }) => (
<Icon name="home" size={24} color={tintColor} />)
},

Please try to implement this way. This is copy of my tabNavigator. Hope this will be helpful for you.
const TabRouter = createBottomTabNavigator(
{
HomeAfterLoginScreen: { screen: A },
ShowListAlertScreen: { screen: B },
ShowListProfessionScreen: { screen: C },
MyAccountScreen: { screen: F }
},
{
tabBarPosition: "bottom",
tabBarOptions: {
style: { backgroundColor: "#50bcb8" },
showIcon: true,
showLabel: true,
gesturesEnabled: true,
indicatorStyle: { borderBottomWidth: 3, borderBottomColor: Style.color },
inactiveTintColor: "#fff",
activeTintColor: "#fff",
tabStyle: { justifyContent: "center", alignItems: "center" }
}
});

I got it. I have solved this issue.
const App = createBottomTabNavigator(
{
Favorite: {
screen: FavoriteShops,
navigationOptions: {
tabBarLabel: 'お気に入り',
tabBarIcon: ({ tintColor }) => (
<Icon name="heart" size={25} color={tintColor} />
),
},
},
Home: {
screen: Home,
navigationOptions: {
tabBarLabel: 'ホーム',
tabBarIcon: ({ tintColor }) => (
<Icon name="home" size={30} color={tintColor} />
),
},
},
Help: {
screen: Help,
navigationOptions: {
tabBarLabel: 'その他',
tabBarIcon: ({ tintColor }) => (
<Icon name="bars" size={25} color={tintColor} />
),
},
},
},
{
swipeEnabled: false, //Android用
tabBarOptions: {
showIcon: true,
showLabel: true,
activeTintColor: '#DE628D',
inactiveTintColor: '#707070',
style: {
width: '100%',
height: 70,
},
tabStyle: {
paddingTop: 20,
},
},
},
);

Related

React Native - making a second tab bar navigator for a modal screen

Building out a relatively simple app on React Native, I currently have a tabBarNavigator where one of the icons on the tab bar opens a pop-up modal. I'm then trying to create another, separate, tab bar at the bottom of the modal.
However although I have created a tabBarNavigator and appContainer in my root App.js and have exported this container (passing it through my modal component), the modal then loads the base stack rather than the modal stack which I created - obviously not desired behavior!
The relevant portion of App.js:
const ModalNavigator = createBottomTabNavigator({
Photo: { screen: PrivacySettings,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <Feather name="camera" size={24} color="black" />
}
},
Camera: { screen: HelpSettings,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <Feather name="type" size={24} color="black" />
}
},
Text: { screen: ContactUs,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <Feather name="type" size={24} color="black" />
}
}
});
export const ModalContainer = createAppContainer(ModalNavigator);
const AppContainer = createStackNavigator({
default: createBottomTabNavigator({
Home: { screen: Home,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <FontAwesomeIcon size={30} icon={faHome} color={tintColor}/>
}
},
Search: { screen: Contacts,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <FontAwesomeIcon size={30} icon={faSearch} color={tintColor}/>
}
},
AddPhoto: { screen: () => null,
navigationOptions: {
headerTitle: "Upload Photo",
tabBarIcon: <AddPhotoButton/>,
headerMode: 'none',
}
},
Likes: {screen: Vault,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <FontAwesomeIcon size={30} icon={faHeart} color={tintColor}/>
}
},
Settings: {screen: Me,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <FontAwesomeIcon size={30} icon={faUser} color={tintColor}/>
}
}
},
{
defaultNavigationOptions: {
tabBarOnPress: ({ navigation, defaultHandler }) => {
if (navigation.state.key === 'AddPhoto') {
navigation.navigate('addPhotoModal')
} else {
defaultHandler()
}
},
cardStyle: {
backgroundColor: "transparent",
opacity: 1
}
},
tabBarOptions: {
showLabel: false,
activeTintColor: '#1A86CB',
inactiveTintColor: 'black'
},
initialRouteName: "Home",
}),
addPhotoModal: {
screen: AddPhotoModal }
}, {
mode: 'modal',
headerMode: 'none',
transparentCard: true,
}
)
const Routes = createStackNavigator({
Home: { screen: AppContainer,
navigationOptions: {
headerShown: false }
},
SignIn: { screen: SignIn },
AddContact: { screen: AddContact,
navigationOptions: {
headerTitle: "Add Contact" }
},
ContactDetails: {screen: ContactDetails },
PrivacySettings: {screen: PrivacySettings,
navigationOptions: {
headerTitle: "Privacy",
headerStyle: {
backgroundColor: 'white',
shadowColor: 'transparent'
}}
},
NotificationSettings: {screen: NotificationSettings,
navigationOptions: {
headerTitle: "Notifications",
headerStyle: {
backgroundColor: 'white',
shadowColor: 'transparent'
}}
},
HelpSettings: {screen: HelpSettings,
navigationOptions: {
headerTitle: "Help",
headerStyle: {
backgroundColor: 'white',
shadowColor: 'transparent'
}}
},
InviteSettings: {screen: InviteSettings,
navigationOptions: {
headerTitle: "Invite Friends",
headerStyle: {
backgroundColor: 'white',
shadowColor: 'transparent'
}}
},
ContactUs: {screen: ContactUs,
navigationOptions: {
headerTitle: "Contact Us",
headerStyle: {
backgroundColor: 'white',
shadowColor: 'transparent'
}}
},
},
{ initialRouteName: "Home",
});
const AuthStack = createStackNavigator({
Login: SignIn,
})
export default createAppContainer(
createSwitchNavigator(
{
Loading: LoadingScreen,
App: Routes,
Auth: AuthStack
},
{
initalRouteName: LoadingScreen
}
)
)
The relevant portion of the Modal Component:
import ModalContainer from '../../App'
export default class AddPhotoModal extends React.Component {
render() {
return(
<View style={{backgroundColor:"#000000CC", flex:1}}>
<View style={{ backgroundColor:"#ffffff", marginLeft: 0, marginRight: 0, marginTop: 240, padding: 20, borderRadius: 20, flex: 1, }}>
<View style={styles.header}>
<TouchableOpacity style={{position: 'absolute'}} onPress={() => this.props.navigation.goBack()}>
<Text style={styles.buttonFont}>Back</Text>
</TouchableOpacity>
<TouchableOpacity style={{position: 'absolute', right: 0}} onPress={() => this.props.navigation.navigate('UploadScreen')}>
<Text style={styles.buttonFont}>Continue</Text>
</TouchableOpacity>
</View>
<ModalContainer/>
</View>
</View>
);
}
}
Edit:
With the below answer, I'm now getting this. Is there a way to place the tabbar as a child of the modal component?
https://ibb.co/kcTq0X3
'ModalContainer' is not a part of the 'Routes', So it is not known to the SwitchNavigator.
Add ModalContainer as a part of 'Routes' stack.
And to show the ModalContainer bottomTabs, navigate to ModalContainer on click of the tabIcon

React Drawer Navigation menu icon is not showing

Please note that this is my route configuration with the "Stack, Drawer and Tab Navigation"
Stack and the tab navigation is working fine. But the drawer icon is not showing at all and the drawer is working only If I slide the window manually. How to show the drawer icon and use it to open the drawer window? Please help me to sort this out.
import React from "react"
import { createAppContainer } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'
import { createBottomTabNavigator } from 'react-navigation-tabs'
import { createDrawerNavigator } from 'react-navigation-drawer'
import Icon from 'react-native-vector-icons/Feather'
import HomeScreen from './screens/home/HomeScreen'
import ControlScreen from './screens/controls/ControlScreen'
import MoreScreen from './screens/more/MoreScreen'
import SignUpScreen from './screens/signup/SignUpScreen'
import AboutScreen from './screens/about/AboutScreen'
const TabContainer = createBottomTabNavigator({
Home: {
screen: HomeScreen,
navigationOptions: {
tabBarIcon: ({ tintColor, focused }) => <Icon name="home" size={20} color={`${focused ? tintColor : '#404040'}`} />,
}
},
Controls: {
screen: ControlScreen,
navigationOptions: {
tabBarIcon: ({ tintColor, focused }) => <Icon name="sliders" size={20} color={`${focused ? tintColor : '#404040'}`} />
}
},
More: {
screen: MoreScreen,
navigationOptions: {
tabBarIcon: ({ tintColor, focused }) => <Icon name="more-horizontal" size={20} color={`${focused ? tintColor : '#404040'}`} />
}
},
},{
tabBarposition: 'bottom',
swipeEnabled: false,
tabBarOptions: {
activeTintColor: '#069',
inactiveTintColor: '#404040',
labelStyle: {
fontSize: 14,
paddingTop: 0,
paddingBottom: 0
},
style:{
borderTopWidth: 1,
borderTopColor:'#333333',
position: 'absolute',
bottom: 0,
left: 0
},
animationEnabled: false,
tabStyle: {
paddingBottom: 15,
paddingTop: 0,
height: 75,
marginBottom: 0,
},
}
}
)
const StackContainer = createStackNavigator({
TabContainer,
},{
defaultNavigationOptions: {
headerShown: false
}
})
const MainDrawerNavigator = createDrawerNavigator({
Home: {
screen: StackContainer,
headerStyle: {
backgroundColor: '#f4511e',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
title: 'Home',
navigationOptions: {
drawerIcon: ({ tintColor }) => (
<Icon name="home" size={20} color={'#404040'} />
),
}
},
SignUp: {
screen: SignUpScreen,
navigationOptions: {
drawerIcon: ({ tintColor }) => (
<Icon name="user" size={20} color={'#404040'} />
),
}
},
About: {
screen: AboutScreen,
navigationOptions: {
drawerIcon: ({ tintColor }) => (
<Icon name="alert-circle" size={20} color={'#404040'} />
),
}
},
},
{
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle',
navigationOptions: {
drawerLockMode: 'locked-closed',
},
drawerPosition: 'left',
headerStyle: { backgroundColor: '#E73536' },
headerTintColor: 'white',
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle',
},
)
export default createAppContainer(MainDrawerNavigator)
You need to include your drawerNavigator inside stackNavigator and you can provide the default navigation options there.
I had edited your code check this:
import React from "react"
import { createAppContainer } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'
import { createBottomTabNavigator } from 'react-navigation-tabs'
import { createDrawerNavigator } from 'react-navigation-drawer';
import Icon from 'react-native-vector-icons/Feather'
import HomeScreen from './screens/Example'
import ControlScreen from './screens/Main'
import MoreScreen from './screens/Sample'
import SignUpScreen from './screens/login'
import AboutScreen from './screens/login'
const TabContainer = createBottomTabNavigator({
Home: {
screen: HomeScreen,
navigationOptions: {
tabBarIcon: ({ tintColor, focused }) => <Icon name="home" size={20} color={`${focused ? tintColor : '#404040'}`} />,
}
},
Controls: {
screen: ControlScreen,
navigationOptions: {
tabBarIcon: ({ tintColor, focused }) => <Icon name="sliders" size={20} color={`${focused ? tintColor : '#404040'}`} />
}
},
More: {
screen: MoreScreen,
navigationOptions: {
tabBarIcon: ({ tintColor, focused }) => <Icon name="more-horizontal" size={20} color={`${focused ? tintColor : '#404040'}`} />
}
},
},{
tabBarposition: 'bottom',
swipeEnabled: false,
tabBarOptions: {
activeTintColor: '#069',
inactiveTintColor: '#404040',
labelStyle: {
fontSize: 14,
paddingTop: 0,
paddingBottom: 0
},
style:{
borderTopWidth: 1,
borderTopColor:'#333333',
position: 'absolute',
bottom: 0,
left: 0
},
animationEnabled: false,
tabStyle: {
paddingBottom: 15,
paddingTop: 0,
height: 75,
marginBottom: 0,
},
}
}
)
const MainDrawerNavigator = createDrawerNavigator({
Home: {
screen: TabContainer,
headerStyle: {
backgroundColor: '#f4511e',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
title: 'Home',
navigationOptions: {
drawerIcon: ({ tintColor }) => (
<Icon name="home" size={20} color={'#404040'} />
),
}
},
SignUp: {
screen: SignUpScreen,
navigationOptions: {
drawerIcon: ({ tintColor }) => (
<Icon name="user" size={20} color={'#404040'} />
),
}
},
About: {
screen: AboutScreen,
navigationOptions: {
drawerIcon: ({ tintColor }) => (
<Icon name="alert-circle" size={20} color={'#404040'} />
),
}
},
},{
initialRouteName:"Home"
}
)
const StackContainer = createStackNavigator({
MainDrawerNavigator,
}, {
defaultNavigationOptions: ({ navigation }) => {
return {
headerTitle:"Welcome!",
headerLeft: (
<Icon
style={{ paddingLeft: 10 }}
onPress={() => navigation.openDrawer()}
name="menu"
size={30}
/>
)
};
}
})
export default createAppContainer(StackContainer)
Hope this helps!

TabIcon count not updated if record is deleted on tab focus in react-native CLI

Regarding my question from SO: Mapping view not updating on tab navigator in react-native, I have added function to delete the product from cart. And yes the cart view is reducing after deleting, but the tabicon badge count is still on last count, until I press on any tab other than cart.
For adding product cart to object, yes it will update the count accordingly. Just when delete record from object, the count is not updated.
My script on cart screen has follow suggestion from linked posting:-
componentDidUpdate(prevProps) {
if (prevProps.isFocused !== this.props.isFocused) {
this.setState({ prodData: appGlobal.ObjProduct });
}
}
shouldComponentUpdate(nextProps, nextState) {
if (this.props.prodData !== nextState.prodData) {
return true;
}
return false;
}
On TabHelpers class:-
const tabScreen = createBottomTabNavigator(
{
'Home': {
screen: homeStack,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name='home' size={20} color={tintColor} />
),
},
},
'Favourite': {
screen: favStack,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name='heart' size={20} color={tintColor} />
),
},
},
'Cart': {
screen: cartStack,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<View>
<Icon name='shopping-cart' size={20} color={tintColor} />
{typeof appGlobal.ObjProduct !== 'undefined' && appGlobal.ObjProduct.length > 0 ?
<Badge style={{ position: 'absolute', right: 10, top: 0, height: 13 }}>
<Text style={{ color: 'white', fontSize: 8 }}>{appGlobal.ObjProduct.length}</Text>
</Badge>
: null}
</View>
),
},
},
'Profile': {
screen: profileStack,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name='user' size={20} color={tintColor} />
),
},
}
},
{
initialRouteName: 'Home',
headerMode: 'none',
lazy: false,
backBehavior: 'initialRoute',
swipeEnabled: true,
animationEnabled: true,
tabBarPosition: 'bottom',
tabBarOptions: {
showLabel: true,
showIcon: true,
activeTintColor: '#ffffff',
activeBackgroundColor: '#4961c4',
inactiveTintColor: '#4962a6',
style: {
backgroundColor: '#475691',
},
labelStyle: {
textAlign: 'center',
fontWeight: 'bold',
},
indicatorStyle: {
borderBottomColor: '#FFFFFF',
borderBottomWidth: 2,
},
},
defaultNavigationOptions: ({ navigation }) => ({
tabBarOnPress: ({ navigation, defaultHandler }) => {
navigation.popToTop();
defaultHandler();
},
}),
}
);
export default createAppContainer(tabScreen);
Did I miss something or wrongly apply?

react-navigation-tabs - icons not aligned and covering text

I cant seem to get the icons to align using the new react-navigation-tabs.....some icons are sitting higher than others.
Also the icons are covering the labels and Id like to have some margin between icon and label.
I tried the code style={{textAlignVertical: 'center'}} for the icons (from another question on SO) but that did not work either
Below is code
import {createMaterialTopTabNavigator} from 'react-navigation-tabs';
import IconFA from 'react-native-vector-icons/FontAwesome';
import IconMCI from 'react-native-vector-icons/MaterialCommunityIcons';
const ProfileTabBarIcon = ({tintColor}) => (
<IconFA
name="user-circle"
size={35}
color={tintColor}
/>
);
const SearchTabBarIcon = ({tintColor}) => (
<IconMCI
name="account-search"
size={45}
color={tintColor}
/*onPress={() => {
console.log('HELP!!');
this.props.navigation.navigate('Search');
}}*/
/>
);
const MessageTabBarIcon = ({tintColor}) => (
<IconFA
name="envelope"
size={35}
color={tintColor}
/>
);
const SignedInTabNav = createMaterialTopTabNavigator(
{
Profile: {
screen: Profile,
navigationOptions: {
tabBarLabel: 'Me',
tabBarIcon: ProfileTabBarIcon,
},
},
Search: {
screen: Search,
navigationOptions: {
tabBarLabel: 'Search',
tabBarIcon: SearchTabBarIcon,
},
},
Message: {
screen: Message,
navigationOptions: {
tabBarLabel: 'Message',
tabBarIcon: MessageTabBarIcon,
},
},
},
{
tabBarOptions: {
showIcon: true,
upperCaseLabel: false,
activeTintColor: '#42CBC8',
inactiveTintColor: '#9A9F99',
iconStyle: {
width: 'auto',
height: 20,
},
labelStyle: {
fontSize: 12,
},
style: {
backgroundColor: '#F8F8FF',
//borderBottomWidth: 2,
borderBottomColor: '#D3D3D3',
paddingVertical: 2,
height: 60,
},
},
animationEnabled: false,
},
);
....but this is what it looks like :(
Can anyone help?
I styled the first icon and you can change it as necessary to fit your needs and then apply those to the other two icons as well. If you need to understand what is going on better go ahead and apply backgroundColor attr to different elements. Simply, I wrapped the icon component in a View and then gave that view some styling to fit the icon appropriately.
import {createMaterialTopTabNavigator} from 'react-navigation-tabs';
import IconFA from 'react-native-vector-icons/FontAwesome';
import IconMCI from 'react-native-vector-icons/MaterialCommunityIcons';
import HomeScreen from '../screens/HomeScreen';
import LinksScreen from '../screens/LinksScreen';
import SettingsScreen from '../screens/SettingsScreen';
import React from 'react';
import { StyleSheet, View } from 'react-native';
const ProfileTabBarIcon = ({tintColor}) => (
<View style={styles.container}>
<IconFA
style={styles.iconStyle}
name="user-circle"
size={35}
color={tintColor}
/>
</View>
);
const SearchTabBarIcon = ({tintColor}) => (
<IconMCI
name="account-search"
size={45}
color={tintColor}
/*onPress={() => {
console.log('HELP!!');
this.props.navigation.navigate('Search');
}}*/
/>
);
const MessageTabBarIcon = ({tintColor}) => (
<IconFA
name="envelope"
size={35}
color={tintColor}
/>
);
export default SignedInTabNav = createMaterialTopTabNavigator(
{
Profile: {
screen: HomeScreen,
navigationOptions: {
tabBarLabel: 'Me',
tabBarIcon: ProfileTabBarIcon,
},
},
Search: {
screen: LinksScreen,
navigationOptions: {
tabBarLabel: 'Search',
tabBarIcon: SearchTabBarIcon,
},
},
Message: {
screen: SettingsScreen,
navigationOptions: {
tabBarLabel: 'Message',
tabBarIcon: MessageTabBarIcon,
},
},
},
{
tabBarOptions: {
showIcon: true,
upperCaseLabel: false,
activeTintColor: '#42CBC8',
inactiveTintColor: '#9A9F99',
labelStyle: {
fontSize: 12,
margin: 0
},
iconStyle: {
flex: 1
},
style: {
backgroundColor: '#F8F8FF',
height: 65,
borderBottomColor: '#D3D3D3',
},
},
animationEnabled: false,
},
);
const styles = StyleSheet.create({
container: {
flex: 1,
width: 50,
alignItems: 'center',
justifyContent: 'center',
alignSelf: 'center',
}
});
I added an empty tabBarLabel in the tab screen, and included the text inside the tabBarIcon.
<Tab.Screen
name="Home"
component={MainStackNavigator}
options=
{{
tabBarLabel:"",
tabBarIcon:() =>
{
return(
<View>
<Text>Home</Text>
<Image
style={{ width:20,height:20 }}
source{require('./assets/home_white.png')}>
</Image>
</View>
)
}
}}
/>
set resizeMode in your style section
such as :
<Image
source={iconName}
style={{ width: 25, height: 25, tintColor }}
resizeMode={"contain"}
/>

Indicator is not rendering on iOS and Android

I am trying to render the topBar on tab navigation and it's not showing
import { createBottomTabNavigator, createAppContainer } from "react-navigation";
const TabNavigator = createBottomTabNavigator(
{
Dashboard: {
screen: Dashboard,
navigationOptions: {
title: "Dashboard",
tabBarIcon: ({ tintColor }) => (
<Icon
name="home"
type="Entypo"
style={{
color: tintColor,
}}
color={tintColor}
/>
),
},
},
},
{
tabBarPosition: "top",
tabBarOptions: {
activeTintColor: "#5D10F6",
inactiveTintColor: "gray",
showIcon: true,
tintColor: "#333",
animationEnabled: false,
showLabel: false,
scrollEnabled: true,
tabBarPosition: "top",
indicatorStyle: {
backgroundColor: "red",
},
},
}
);
Note: createTabNavigator is removed in react-navigation#3.x. Please use createBottomTabNavigator and/or createMaterialTopTabNavigator instead.
However createBottomTabNavigator no longer supports indicatorStyle :(