Search code examples
javascriptreactjsreact-nativetabbarusability

How to remove bottom taskbar on a certain screen in React Native?


I have added a chat to my app with react-native-gifted-chat and I want to remove the taskbar (tabBar) on the specific chat screen, to offer more space and a better user experience.

This happens on iOS and Android

But I can't hide it, despite trying different ways to do it:

  • Add tabBarVisible: false,

  • I have added my function

    const getTabBarVisibility = (route) => { const routeName = route.state ? route.state.routes [route.state.index] .name : '';

     if (routeName === 'Chat') {
       return false
     }
     return true
    

    }

  • I have added react-navigation options: (https://github.com/react-navigation/react-navigation/issues/7677)

    const getTabBarVisible = (route) => { const routeName = route.state ? route.state.routes [route.state.index] .name : route.params? .screen || 'Home';

     if (routeName === 'Details') {
       return false;
     }
     return true;
    

    }

But I can't get tabBar to hide on this screen.

I show screenshots and the code I have tried to fix this:

const MessageStack = ({ navigation }) => (
  <Stack.Navigator>
    <Stack.Screen name="Messages" component={MensajeScreen} />
    <Stack.Screen
      name="Chat"
      component={ChatScreen}
      options={({ route }) => ({
        //tabBarVisible: route.state && route.state.index === 0,
        title: route.params.userName,
        headerBackTitleVisible: false,
        //tabBarVisible:false
      })}
    />
  </Stack.Navigator>
)

const ProfileStack = ({ navigation }) => (
  <Stack.Navigator>
    <Stack.Screen
      name="Profile"
      component={ProfileScreen}
      options={{
        headerShown: false,
      }}
    />
  </Stack.Navigator>
)

const AppStack = () => {

  const getTabBarVisible = (route) =>{
    const routeName = route.state
      ?  route.state.routes[route.state.index].name
      : route.params?.screen || 'Home';
  
    if (routeName === 'Details') {
      return false;
    }
    return true;
  }

  return (
    <Tab.Navigator
    screenOptions={{
      activeTintColor: '#2e64e5'
    }}>
      <Tab.Screen
        name="Home"
        component={FeedStack}
        options={({ route }) => ({
          tabBarLabel: 'Home',
          headerShown: false,
          tabBarVisible: route.state && route.state.index === 0,
          tabBarIcon: ({ color, size }) => (
            <MaterialCommunityIcons
              name="home-outline"
              color={color}
              size={size}
            />
          ),
        })}
      />
      <Tab.Screen
        name="Messages"
        component={MessageStack}
        options={({ route }) => ({
          tabBarVisible: getTabBarVisible(route),
          //tabBarVisible:false,
          //tabBarVisible: getTabBarVisibility(route),
          tabBarVisible: route.state && route.state.index === 0,
          headerShown: false,
          tabBarIcon: ({ color, size }) => (
            <Ionicons
              name="chatbox-ellipses-outline"
              color={color}
              size={size}
            />
          ),
        })}
      />
      <Tab.Screen
        name="Profile"
        component={ProfileStack}
        options={{
          headerShown: false,
          tabBarIcon: ({ color, size }) => (
            <Ionicons name="person-outline" color={color} size={size} />
          ),
        }}
      />
    </Tab.Navigator>
  )
}

export default AppStack

///////////////////////////////////////////////

<Stack.Screen
      name="HomeProfile"
      component={ProfileScreen}
      options={{
        headerTitleAlign: 'center',
        headerStyle: {
          backgroundColor: '#fff',
          shadowColor: '#fff',
          elevation: 0,
        },
        headerBackTitleVisible: false,
        headerBackImage: () => (
          <View style={{ marginLeft: 15 }}>
            <Ionicons name="arrow-back" size={25} color="#2e64e5" />
          </View>
        ),
      }}
    />
  </Stack.Navigator>
);

const MessageStack = ({ navigation }) => (
  <Stack.Navigator>
    <Stack.Screen name="Messages" component={MensajeScreen} />
    <Stack.Screen
      name="Chat"
      component={ChatScreen}
      options={({ route }) => ({
        //tabBarVisible: route.state && route.state.index === 0,
        title: route.params.userName,
        headerBackTitleVisible: false,
        //tabBarVisible:false
      })}
    />
  </Stack.Navigator>
)

const ProfileStack = ({ navigation }) => (
  <Stack.Navigator>
    <Stack.Screen
      name="Profile"
      component={ProfileScreen}
      options={{
        headerShown: false,
      }}
    />
  </Stack.Navigator>
)

const AppStack = () => {
  const getTabBarVisibility = (route) => {
    const routeName = route.state
      ? route.state.routes[route.state.index].name
      : '';

    if (routeName === 'Chat') {
      return false
    }
    return true
  }

  return (
    <Tab.Navigator
    screenOptions={{
      activeTintColor: '#2e64e5'
    }}>
      <Tab.Screen
        name="Home"
        component={FeedStack}
        options={({ route }) => ({
          tabBarLabel: 'Home',
          headerShown: false,
          // tabBarVisible: route.state && route.state.index === 0,
          tabBarIcon: ({ color, size }) => (
            <MaterialCommunityIcons
              name="home-outline"
              color={color}
              size={size}
            />
          ),
        })}
      />
      <Tab.Screen
        name="Messages"
        component={MessageStack}
        options={({ route }) => ({
          tabBarVisible: getTabBarVisible(route),
          //tabBarVisible:false,
          tabBarVisible: getTabBarVisibility(route),
        //tabBarVisible: route.state && route.state.index === 0,
          headerShown: false,
          tabBarIcon: ({ color, size }) => (
            <Ionicons
              name="chatbox-ellipses-outline"
              color={color}
              size={size}
            />
          ),
        })}
      />
      <Tab.Screen
        name="Profile"
        component={ProfileStack}
        options={{
          headerShown: false,
          // tabBarLabel: 'Home',
          tabBarIcon: ({ color, size }) => (
            <Ionicons name="person-outline" color={color} size={size} />
          ),
        }}
      />
    </Tab.Navigator>
  )
}

export default AppStack

////////////////////////////////////

I have added more code

import React from 'react'
import { View, TouchableOpacity, Text } from 'react-native'
import { createStackNavigator } from '@react-navigation/stack'
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons'
import Ionicons from 'react-native-vector-icons/Ionicons'
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5'

import HomeScreen from '../screens/HomeSreen'
import ChatScreen from '../screens/ChatScreen'
import ProfileScreen from '../screens/ProfileScreen'
import AddPostScreen from '../screens/AddPostScreen'
import MensajeScreen from '../screens/MensajeScreen'
import EditarPerfilScreen from '../screens/EditarPerfilScreen'

const Stack = createStackNavigator()
const Tab = createBottomTabNavigator()

const FeedStack = ({ navigation }) => (
  <Stack.Navigator>
    <Stack.Screen
      name="Vinkylim Network"
      component={HomeScreen}
      options={{
        headerTitleAlign: 'center',
        headerTitleStyle: {
          color: '#2e64e5',
          fontFamily: 'Kufam-SemiBoldItalic',
          fontSize: 18,
        },
        headerStyle: {
          shadowColor: '#fff',
          elevation: 0,
        },
        headerRight: () => (
          <View style={{ marginRight: 10 }}>
            <FontAwesome5.Button
              name="plus"
              size={22}
              backgroundColor="#fff"
              color="#2e64e5"
              onPress={() => navigation.navigate('AddPost')}
            />
          </View>
        ),
      }}
    />
    <Stack.Screen
      name="AddPost"
      component={AddPostScreen}
      options={{

        headerTitleAlign: 'center',
        headerStyle: {
          backgroundColor: '#2e64e515',
          shadowColor: '#2e64e515',
          elevation: 0,
        },
        headerBackTitleVisible: false,
        headerBackImage: () => (
          <View style={{ marginLeft: 15 }}>
            <Ionicons name="arrow-back" size={25} color="#2e64e5" />
          </View>
        ),
      }}
    />
    <Stack.Screen
      name="HomeProfile"
      component={ProfileScreen}
      options={{
        //tabBarVisible:false,
        headerTitleAlign: 'center',
        headerStyle: {
          backgroundColor: '#fff',
          shadowColor: '#fff',
          elevation: 0,
        },
        headerBackTitleVisible: false,
        headerBackImage: () => (
          <View style={{ marginLeft: 15 }}>
            <Ionicons name="arrow-back" size={25} color="#2e64e5" />
          </View>
        ),
      }}
    />
  </Stack.Navigator>
);

const MessageStack = ({ navigation }) => (
  <Stack.Navigator>
    <Stack.Screen name="Messages" component={MensajeScreen} />
    <Stack.Screen
      name="Chat"
      component={ChatScreen}
      options={({ route }) => ({
        //tabBarVisible: route.state && route.state.index === 0,
        title: route.params.userName,
        headerBackTitleVisible: false,
        //tabBarVisible:false
      })}
    />
  </Stack.Navigator>
)

const ProfileStack = ({ navigation }) => (
  <Stack.Navigator>
    <Stack.Screen
      name="Profile"
      component={ProfileScreen}
      options={{
        headerShown: false,
      }}
    />
    <Stack.Screen
      name="EditProfile"
      component={EditarPerfilScreen}
      options={{
        headerTitle: 'Edit Profile',
        headerBackTitleVisible: false,
        headerTitleAlign: 'center',
        headerStyle: {
          backgroundColor: '#fff',
          shadowColor: '#fff',
          elevation: 0,
        },
      }}
    />
  </Stack.Navigator>
)

const AppStack = () => {
  const getTabBarVisibility = (route) => {
    const routeName = route.state
      ? route.state.routes[route.state.index].name
      : '';

    if (routeName === 'Chat') {
      return false
    }
    return true
  }

  /* const getTabBarVisible = (route) =>{
    const routeName = route.state
      ?  route.state.routes[route.state.index].name
      : route.params?.screen || 'Home';
  
    if (routeName === 'Details') {
      return false;
    }
    return true;
  } */

  return (
    <Tab.Navigator
    screenOptions={{
      activeTintColor: '#2e64e5'
    }}>
      <Tab.Screen
        name="Home"
        component={FeedStack}
        options={({ route }) => ({
          tabBarLabel: 'Home',
          headerShown: false,
          // tabBarVisible: route.state && route.state.index === 0,
          tabBarIcon: ({ color, size }) => (
            <MaterialCommunityIcons
              name="home-outline"
              color={color}
              size={size}
            />
          ),
        })}
      />
      <Tab.Screen
        name="Messages"
        component={MessageStack}
        options={({ route }) => ({
          //tabBarVisible: getTabBarVisible(route),
          //tabBarVisible:false,
          tabBarVisible: getTabBarVisibility(route),
        tabBarVisible: route.state && route.state.index === 0,
          headerShown: false,
          tabBarIcon: ({ color, size }) => (
            <Ionicons
              name="chatbox-ellipses-outline"
              color={color}
              size={size}
            />
          ),
        })}
      />
      <Tab.Screen
        name="Profile"
        component={ProfileStack}
        options={{
          headerShown: false,
          // tabBarLabel: 'Home',
          tabBarIcon: ({ color, size }) => (
            <Ionicons name="person-outline" color={color} size={size} />
          ),
        }}
      />
    </Tab.Navigator>
  )
}

export default AppStack

Screenshots

enter image description here

enter image description here

enter image description here


Solution

  • import {getFocusedRouteNameFromRoute} from '@react-navigation/native';  
    
    
    function getTabVisible(route) {
        const routeName = getFocusedRouteNameFromRoute(route) ?? 'Chat';
    
    
      if (routeName === 'Chat') {
        return 'none';
      }
      return 'flex';
    }
    
    
    <Tab.Screen
        options={({route}) => ({            
             tabBarStyle: {display: getTabVisible(route)},
        })
    />