react-navigation : How to hide one specific tabBar icon and label - react-native

I am using react-navigation. I have three icons for my bottomtab navigator and I only want to show two icons(screen1 & screen2) and hide the other(screen3).
How can I hide the icon and the label of screen3?
This is my code:
const ButtomTabNavigator = createBottomTabNavigator(
{
screen1: {
screen: screen1,
navigationOptions: ({ navigation }) => ({
tabBarLabel: 'screen1',
header: null,
tabBarIcon: ({ tintColor }) => (
<Image style={{ width: 18, height: 18, tintColor: tintColor }}
source={require('./Components/Assets/iconMyGrey.png')}
/>
),
})
},
screen2: {
screen: screen2,
navigationOptions: {
header: null,
tabBarLabel: 'screen2',
tabBarIcon: ({ tintColor }) => (
<Image style={{ width: 18, height: 18, tintColor: tintColor }}
source={require('./Components/Assets/iconListGrey.png')}
/>
),
}
},
screen3: {
screen: screen3,
navigationOptions: {
header: null,
tabBarLabel: 'screen3',
tabBarIcon: ({ tintColor }) => (
<Image style={{ width: 21, height: 17, tintColor: tintColor }}
source={require('./Components/Assets/iconReservationMint.png')}
/>
),
}
},
},
{
tabBarOptions: {
activeTintColor: '#16bb92',
},
shifting: true,
}
)
Any advice or comments would be really appreciated! Thanks in advance :)

Please try this
const HomeTab = createBottomTabNavigator(
{
Scree1: Screen1,
Screen2: Screen2,
Screen3: Screen3,
},
{
defaultNavigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, horizontal, tintColor }) => {
const { routeName } = navigation.state;
let iconName;
if (routeName === "Screen1") {
iconName = Images.icons.efficiencyTab;
} else if (routeName === "Screen2") {
iconName = Images.icons.messagesTab;
} else if (routeName === "Screen3") {
return (<View/>)
}
return (
<Image
style={{ width: 30, height: 30, tintColor: tintColor }}
source={iconName}
/>
);
},
tabBarLabel: ({ focused, horizontal, tintColor }) => {
const { routeName } = navigation.state;
let title;
if (routeName === "Screen1") {
title = "Screen1";
} else if (routeName === "Screen2") {
title = "Screen2";
} else if (routeName === "Screen3") {
title = "";
}
return (
<Text
style={{
// alignSelf: "center",
textAlign: "center",
fontSize: 12,
color: tintColor,
marginBottom: 3
}}
>
{title}
</Text>
);
}
}),
tabBarOptions: {
activeTintColor: Constants.APP_THEME_COLOR2,
inactiveTintColor: Constants.APP_GRAY_COLOR,
style: {
height: 50
},
labelStyle: {
marginBottom: 3
}
}
}
);

You can remove screen3 from createBottomTabNavigator and add this screen to RootNavigator. If you want to navigate to screen3 you can navigation.navigate("screen3").In detail https://snack.expo.io/HytREN3ur.

Related

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?

How to get a background image in createBottomTabNavigator?

I have been trying to get a background image for the tabbar. I tried using tabBarComponent but it hides the tabs under it.
The code that I am using is
export default MainNavigation = createBottomTabNavigator(
{
Profile:{
screen: Profile,
navigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, tintColor }) => {
return <Image source={require('./images/tab_explore.png')} />
}
}),
}
},
{
tabBarComponent: props =>{
return(
<View style={{backgroundColor:"black"}}>
<Image
style={{ width:'100%', height: 80 }}
source={ require('./images/bottom_btn.png')} />
</View>
);
}
})
Does anyone know how to solve the problem? Thanks in advance!
Current Output:
It should show the tabs on top of orange color.
Maybe it is too late, but I wish this answer will help many people. In React Navigation version 2 you can do it like this:
import { createBottomTabNavigator, BottomTabBar } from 'react-navigation-tabs';
You don't need to install react-navigation-tabs if you have installed 'react-navigation'.
Then create TabBarComponent by const TabBarComponent = (props) => (<BottomTabBar {...props} />);. You will use tabBarComponent key in createBottomTabNavigator. BackgroundColor: 'transparent' for default tabs and VOILA!
The following code will give you the main idea
export const Tabs = createBottomTabNavigator({
Posters: {
screen: Posters,
navigationOptions: {
tabBarLabel: 'AFİŞA',
tabBarIcon: ({tintColor}) => (
<MaterialIcons name="local-movies" size={24} color={tintColor} />
)
}
},
ComingSoon: {
screen: ComingSoon,
navigationOptions: {
tabBarLabel: 'TEZLİKLƏ',
tabBarIcon: ({tintColor}) => (
<MaterialIcons name="movie" size={24} color={tintColor} />
)
}
},
Tickets: {
screen: Tickets,
navigationOptions: {
tabBarLabel: 'BİLETLƏR',
tabBarIcon: ({tintColor}) => (
<MaterialIcons name="confirmation-number" size={24} color={tintColor} />
),
}
},
More: {
screen: More,
navigationOptions: {
tabBarLabel: 'MENU',
tabBarIcon: ({tintColor}) => (
<MaterialIcons name="more-horiz" size={24} color={tintColor} />
),
tabBarOnPress: ({ navigation }) => {
return <Text>sd</Text>
}
}
},
},
{
order: ['Posters', 'ComingSoon', 'Tickets', 'More' ],
tabBarOptions: {
activeTintColor: colors.darkYellow(),
inactiveTintColor: 'white',
labelStyle: {
fontSize: 12,
fontWeight: 'bold',
fontFamily: defaults.fontFamily
},
style: styles.tabBar,
},
tabBarComponent: props =>{
return(
<React.Fragment>
<TabBarComponent {...props} />
<Image style={styles.fakeTabBackground}/>
</React.Fragment>
)
}
})
I wish I could comment but I don't have enough reputation to do that.
<React.Fragment>
<ImageBackground
source={require('./images/bottom_btn.png')}
style={{width: '100%', height: 80}}
>
<TabBarComponent {...props} />
</ImageBackground>
</React.Fragment>
This worked for me. Spent almost a day figuring out the <ImageBackground> thing.
With the latest version of the 'react-navigation-tabs' the above solutions don't work, what we need to do is, we should pass the following three properties to the BottomTabBar component explicitly:
<BottomTabBar
{...this.props}
getButtonComponent={this.getButtonComponent}
getAccessibilityRole={() => 'button'}
getAccessibilityStates={({focused}) => (focused ? ['selected'] : [])}
/>
So the full code for this example is as follows :
class TabBar extends React.Component {
render() {
return (
<BottomTabBar
{...this.props}
getButtonComponent={this.getButtonComponent}
getAccessibilityRole={() => 'button'}
getAccessibilityStates={({focused}) => (focused ? ['selected'] : [])}
/>
);
}
getButtonComponent({route}) {
if (route.key === 'Other') {
return () => <View />; // a view that doesn't render its children
} else {
return null; // use the default nav button component
}
}
}
const Tabs = createMaterialTopTabNavigator(
{
home: {
screen: Home_StackNavigator,
navigationOptions: ({navigation}) => ({
title: 'home',
tabBarIcon: ({tintColor}) => (
<FontAwesome name="home" size={23} color={tintColor} />
),
}),
},
favourites: {
screen: Favourites_StackNavigator,
navigationOptions: ({navigation}) => ({
title: 'favourites',
tabBarIcon: ({tintColor}) => (
<Ionicons name="md-star" size={25} color={tintColor} />
),
}),
},
calendar: {
screen: Calendar_StackNavigator,
navigationOptions: ({navigation}) => ({
title: 'calendar',
tabBarIcon: ({tintColor}) => (
<Feather name="calendar" size={23} color={tintColor} />
),
}),
},
shoppingList: {
screen: ShoppingList_StackNavigator,
navigationOptions: ({navigation}) => ({
title: 'shopping List',
tabBarIcon: ({tintColor}) => (
<Feather name="shopping-bag" size={23} color={tintColor} />
),
}),
},
profile: {
screen: Profile_StackNavigator,
navigationOptions: ({navigation}) => ({
title: 'profile',
tabBarIcon: ({tintColor}) => (
<Feather name="user" size={23} color={tintColor} />
),
}),
},
},
{
tabBarPosition: 'bottom',
swipeEnabled: false,
animationEnabled: true,
tabBarOptions: {
showIcon: true,
activeTintColor: Colors.DARK.ICON_ACTIVE,
inactiveTintColor: Colors.DARK.ICON_INACTIVE,
style: {
backgroundColor: 'transparent',
},
labelStyle: {
textAlign: 'center',
fontSize: 10,
textTransform: 'capitalize',
color: Colors.DARK.ICON_INACTIVE,
marginBottom: 1,
width: 70,
},
indicatorStyle: {
borderBottomColor: Colors.DARK.ICON_INACTIVE,
borderBottomWidth: 2,
},
},
tabBarComponent: (props) => {
return (
<React.Fragment>
<ImageBackground
source={require('../../assets/images/image_bottom.jpg')}
style={{width: '100%', height: 60}}>
<TabBar {...props} />
</ImageBackground>
</React.Fragment>
);
},
},
);

React Native - Change background color in tabnavigator dynamically

i want to change my tab navigator background color dynamically in based on my API response so below is my code
_TabNavigator.js
const MyTabnav = TabNavigator({
ScreenOne: {
screen: ({ navigation, screenProps }) => <ScreenOneNav screenProps={{ tabbarNavigation: navigation, ...screenProps }} onNavigationStateChange={null} />,
navigationOptions: {
tabBarLabel: 'ScreenOne',
tabBarIcon: ({ tintColor }) => (
<View style={[styles.tabViewBox]}>
<Text style={[styles.tabText, { color: tintColor }]}>ScreenOne</Text>
</View>
)
}
},
ScreenTwo: {
screen: ({ navigation, screenProps }) => <ScreenOneNav screenProps={{ tabbarNavigation: navigation, ...screenProps, }} onNavigationStateChange={null} />,
navigationOptions: {
tabBarLabel: 'ScreenOne',
tabBarIcon: ({ tintColor }) => (
<View style={[styles.tabViewBox]}>
<Text style={[styles.tabText, { color: tintColor }]}>ScreenTwo</Text>
</View>
)
}
},
ScreenThree: {
screen: ({ navigation, screenProps }) => <StockNotificationNav screenProps={{ tabbarNavigation: navigation, ...screenProps }} onNavigationStateChange={null} />,
navigationOptions: {
tabBarLabel: 'Notifications',
tabBarIcon: ({ tintColor }) => (
<View style={[styles.tabViewBox]}>
<Text style={[styles.tabText, { color: tintColor }]}>ScreenThree</Text>
</View>
)
}
},
},
{
tabBarOptions: {
style: {
backgroundColor: white,
height: 55,
borderTopColor: 'transparent',
borderTopWidth: 1,
paddingRight: 10,
paddingLeft: 10,
borderTopWidth: 1,
borderTopColor: grayPlaceHolder
},
showLabel: false,
showIcon : true,
},
tabBarComponent : TabBarBottom,
initialRouteName: 'ScreenTwo',
tabBarPosition: 'bottom',
animationEnabled: false,
swipeEnabled: false
}, []);
var styles = StyleSheet.create({
tabText: {
fontSize: 10,
fontWeight: "600",
flex: 4,
},
tabViewBox: {
flex: 1,
alignItems: "center",
},
tabIcon: {
flex: 5,
alignSelf: "center",
marginTop: 10
},
});
export default StocksTabNav;
I want to change my tabnavigtor background color in my ScreenTwo.js file which include API response code on it as per that it tabnavigator background color (backgroundColor) should change as black or white as per API response so how can i achieve this? your all suggestions are appreciable
After update code as per Rahul suggests give below warning message
what you can do is make one custom tabBar component and using javaScript immutability concept you can override style of tabBarOptions.
const MyTabnav = TabNavigator({ScreenOne: {
screen: ({ navigation, screenProps }) => <ScreenOneNav screenProps={{ tabbarNavigation: navigation, ...screenProps }} onNavigationStateChange={null} />,
navigationOptions: {
tabBarLabel: 'ScreenOne',
tabBarIcon: ({ tintColor }) => (
<View style={[styles.tabViewBox]}>
<Text style={[styles.tabText, { color: tintColor }]}>ScreenOne</Text>
</View>
)
}
},
ScreenTwo: {
screen: ({ navigation, screenProps }) => <ScreenOneNav screenProps={{ tabbarNavigation: navigation, ...screenProps, }} onNavigationStateChange={null} />,
navigationOptions: {
tabBarLabel: 'ScreenOne',
tabBarIcon: ({ tintColor }) => (
<View style={[styles.tabViewBox]}>
<Text style={[styles.tabText, { color: tintColor }]}>ScreenTwo</Text>
</View>
)
}
},
ScreenThree: {
screen: ({ navigation, screenProps }) => <StockNotificationNav screenProps={{ tabbarNavigation: navigation, ...screenProps }} onNavigationStateChange={null} />,
navigationOptions: {
tabBarLabel: 'Notifications',
tabBarIcon: ({ tintColor }) => (
<View style={[styles.tabViewBox]}>
<Text style={[styles.tabText, { color: tintColor }]}>ScreenThree</Text>
</View>
)
}
},
},
{
tabBarOptions: {
style: {
backgroundColor: white,
height: 55,
borderTopColor: 'transparent',
borderTopWidth: 1,
paddingRight: 10,
paddingLeft: 10,
borderTopWidth: 1,
borderTopColor: grayPlaceHolder
},
showLabel: false,
showIcon : true,
},
//Here Goes Your CustomTabBar Component
tabBarComponent : CustomTabBarComponent,
initialRouteName: 'ScreenTwo',
tabBarPosition: 'bottom',
animationEnabled: false,
swipeEnabled: false
}, []);
CustomTabBarComponent.js
const TabBar = (props) => {
const { navigationState } = props;
let newProps = props;
newProps = Object.assign(
{},
props,
{
style: {
// get value from redux store and set it here
backgroundColor: 'rgba(0,0,0,0.1)',
position: 'absolute',
bottom: 0,
left: 0,
right: 0
},
activeTintColor: '#fff',
inactiveTintColor: '#bbb',
},
);
return <TabBarBottom {...newProps} />;
};
Now You can connect this CustomTabBarComponent with Redux store and can change the value of whatever Property you want.
What you need to do is set your tab component as a function and send the color as a parameter to that function. Try this:
const MyTabnav = color => TabNavigator({
ScreenOne: {
screen: ({ navigation, screenProps }) => <ScreenOneNav screenProps={{ tabbarNavigation: navigation, ...screenProps }} onNavigationStateChange={null} />,
navigationOptions: {
tabBarLabel: 'ScreenOne',
tabBarIcon: ({ tintColor }) => (
<View style={[styles.tabViewBox]}>
<Text style={[styles.tabText, { color: tintColor }]}>ScreenOne</Text>
</View>
)
}
},
ScreenTwo: {
screen: ({ navigation, screenProps }) => <ScreenOneNav screenProps={{ tabbarNavigation: navigation, ...screenProps, }} onNavigationStateChange={null} />,
navigationOptions: {
tabBarLabel: 'ScreenOne',
tabBarIcon: ({ tintColor }) => (
<View style={[styles.tabViewBox]}>
<Text style={[styles.tabText, { color: tintColor }]}>ScreenTwo</Text>
</View>
)
}
},
ScreenThree: {
screen: ({ navigation, screenProps }) => <StockNotificationNav screenProps={{ tabbarNavigation: navigation, ...screenProps }} onNavigationStateChange={null} />,
navigationOptions: {
tabBarLabel: 'Notifications',
tabBarIcon: ({ tintColor }) => (
<View style={[styles.tabViewBox]}>
<Text style={[styles.tabText, { color: tintColor }]}>ScreenThree</Text>
</View>
)
}
},
},
{
tabBarOptions: {
//use the color you passed in the prop here:
style: {
backgroundColor: color,
height: 55,
borderTopColor: 'transparent',
borderTopWidth: 1,
paddingRight: 10,
paddingLeft: 10,
borderTopWidth: 1,
borderTopColor: grayPlaceHolder
},
showLabel: false,
showIcon : true,
},
tabBarComponent : TabBarBottom,
initialRouteName: 'ScreenTwo',
tabBarPosition: 'bottom',
animationEnabled: false,
swipeEnabled: false
}, []);
var styles = StyleSheet.create({
tabText: {
fontSize: 10,
fontWeight: "600",
flex: 4,
},
tabViewBox: {
flex: 1,
alignItems: "center",
},
tabIcon: {
flex: 5,
alignSelf: "center",
marginTop: 10
},
});
export default MyTabNav;
Then where you use MyTabnav pass the color as a param to it. for example
export default class App extends Component<{}> {
constructor(props) {
super(props);
this.state = {
color: 'black'
};
}
getRandomColor = () => {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
};
onPress = () => {
this.setState({
color: this.getRandomColor()
});
};
render() {
const Tabs = MyTabnav(this.state.color);
return (
<View style={{ flex: 1 }}>
<Button onPress={this.onPress} title="Click me" />
<Tabs />
</View>
);
}
}
try using
const tabBarOptions = {
// setting height 'null', and top 0 will change the color of pressed tab
indicatorStyle: {
height: null,
top: 0,
backgroundColor: "red",
borderBottomColor: "black",
borderBottomWidth: 3,
},
activeTintColor: "black",
pressColor: "white",
style: {
backgroundColor: "#ddc8be",
},
labelStyle: { fontSize: 13 },
};

React native navigation custom tabbar icon

Is this possible to have custom component ( Button or what ever ) instead of simple icon in tab bar?
I need to set my tab bar icon dynamically like this
this.props.navigator.setTabButton({
tabIndex: 0,
icon: <Icon name="heart" size={28} /> <--- not appear
});
OR we can use only icons? Any solutions?
Yes, it is possible. I am sharing my solution as code below:
const renderNav = (routeName, focused) => {
return (
<View style={{
flex: 1,
width: 90,
justifyContent: 'center',
alignItems: 'center',
borderTopColor: StylesGeneric.COLORS.primary,
borderTopWidth: focused ? 4 : 0,
}}>
<Text
style={[StylesGeneric.STYLES.footerText]}>
{routeName}
</Text>
</View>
);
};
const customTabs = ({navigation}) => ({
tabBarIcon: ({focused}) => {
const {routeName} = navigation.state;
if (routeName === 'Home') {
return renderNav('Home', focused);
} else if (routeName === 'Profile') {
return renderNav('Profile', focused);
} else if (routeName === 'ProfileOther') {
return renderNav('ProfileOther', focused);
}
},
});
const nav = createBottomTabNavigator(
{
Home: {
screen: Home,
},
Profile: {
screen: Profile,
},
ProfileOther: {
screen: ProfileOther,
},
},
{
defaultNavigationOptions: customTabs,
animationEnabled: true,
swipeEnabled: true,
tabBarPosition: 'bottom',
initialRouteName: 'Home',
tabBarOptions: {
showLabel: false,
style: {
borderTopColor: 'transparent',
backgroundColor: StylesGeneric.COLORS.white,
height: StylesGeneric.FOOTER_HEIGHT,
},
},
},
);
const AppContainer = createAppContainer(nav);