'react-navigation: How to change tabBar color based on current tab

I'm getting started with react-navigation.
How do I change the tabBar background color when I change tab?

Here is some pseudo-code showing what I'm hoping for:

_backgroundColor = function() { 
    switch (this.routeName) {
      case 'tabHome':     return { backgroundColor: '#002663' };
      case 'tabRewards':  return { backgroundColor: '#3F9C35' };
      default:            return { backgroundColor: 'white' }           
    }
}

// Tabs setup
export const TabStack = TabNavigator({
  tabHome:     { screen: HomeStack,       },
  tabRewards:  { screen: RewardsStack,    },
}, {
  tabBarOptions: {
    style: _backgroundColor(),
  }
});

At the minute it's always defaulting to white ( which is fine because my code is wrong :-) so how do I pass in something which triggers this logic either on routeName or iconLabel or whatever.

Any help would be most appreciated.
Thanks in advance.
Cheers



Solution 1:[1]

import React from 'react';
import { Image, View } from 'react-native';
import { StackNavigator, TabNavigator, TabBarBottom } from 'react-navigation';

const Screen = () => <View />;

const Tabs = TabNavigator(
  {
    Tab1: {
      screen: Screen,
      navigationOptions: {
        title: 'Tab1',
        tabBarIcon: ({ tintColor }) =>
          (<Image
              source={require('../images/iconNotificationNew.png')}
              style={[{ tintColor }]}
          />),
      },
    },
    Tab2: {
      screen: Screen,
      navigationOptions: {
        title: 'Tab2',
        tabBarIcon: ({ tintColor }) =>
          (<Image
              source={require('../images/iconNotificationNew.png')}
              style={[{ tintColor }]}
          />),
      },
    },
    Tab3: {
      screen: Screen,
      navigationOptions: {
        title: 'Tab3',
        tabBarIcon: ({ tintColor }) =>
          (<Image
              source={require('../images/iconNotificationNew.png')}
              style={[{ tintColor }]}
          />),
      },
    },
  },
  {
    lazy: true,
    tabBarComponent: props => {
      const backgroundColor = props.position.interpolate({
        inputRange: [0, 1, 2],
        outputRange: ['orange', 'white', 'green'],
      })
      return (
        <TabBarBottom
          {...props}
          style={{ backgroundColor: backgroundColor }}
        />
      );
    },
    swipeEnabled: true,
    animationEnabled: true,
    tabBarPosition: 'bottom',
    initialRouteName: 'Tab2',
    tabBarOptions: {
      activeTintColor: 'blue',
      inactiveTintColor: 'grey',
    },
  },
);

Output

on Tab2 Selection

on Tab1 Selection

on Tab3 Selection

Solution 2:[2]

For react-navigation v6, just add tabBarColor on the screen options

   activeColor="#ffffff"
   barStyle={{
     backgroundColor: "#0071BC",
   }}
   initialRouteName="Home"
   shifting={true}
 >
   <Tab.Screen
     name="Downloads"
     component={Downloads}
     options={{
       tabBarLabel: "Downloads",
       tabBarIcon: ({ color }) => (
         <SimpleLineIcons name="question" size={20} color={color} />
       ),
       tabBarColor: Colors.tutor,
     }}
   />```


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ashok R
Solution 2 Bongani Khoza