react-navigation icon indicating copy to clipboard operation
react-navigation copied to clipboard

How can I selectively hide Tabs with nested StackNavigators on certain screens?

Open KabirKang-zz opened this issue 9 years ago • 62 comments

I have a TabNavigator with three TabView children. Each TabView has a stacknavigator as a child so that the user can visit detail views, etc. I want to hide the tabs on one of the detail views. I have tried registering each navigator in a high level context and then accessing the tab navigator from the screen and doing setParams on it, but it doesn't seem that setParams is doing anything.

What is the best way to selectively hide the tabs?

KabirKang-zz avatar Feb 23 '17 19:02 KabirKang-zz

The tabs:

this.TabNav = TabNavigator( HOME_TABS_ROUTER, { initialRouteName: this.initialTab, tabBarPosition: 'bottom', tabBarComponent: TabView.TabBarBottom, order: [HOME_TABS.FIND_TEACHER_TAB, HOME_TABS.CHAT_TAB, HOME_TABS.DASHBOARD_TAB], lazyLoad: true, swipeEnabled: false, animationEnabled: false, tabBarOptions: { showIcon: true, style: { backgroundColor: Colors.headerBackground, height: normalizeValue(65), padding: normalizeValue(10) }, labelStyle: textStyles.footerLabel, activeTintColor: Colors.brandDefault, inactiveTintColor: Colors.defaultText } } );

A child stack nav:

this.StackNavigator = Router(
{
headerComponent: CustomHeader,
headerMode: 'screen',
initialRouteName: ROUTES.FIND_TEACHER_TAB,
navigationBarOptions: {
header: {
title: () => i18n.t('home.chatHeaderTitle'),
}
}
}
);

KabirKang-zz avatar Feb 23 '17 20:02 KabirKang-zz

In order to hide a tabBar on one of the tabs, you can set this option to true. It's been recently documented. Is that something you were looking for?

grabbou avatar Feb 23 '17 20:02 grabbou

@grabbou but how would you put the tabBar navigation option on a screen that is a part of the stacknavigator.

the stack nav is a child of the tabnavigators. that's why i was trying to find a round-about way to reach the "grandparent" navigator.

KabirKang-zz avatar Feb 23 '17 20:02 KabirKang-zz

and i cant make the profile screen part of the tabnavigator because that would make it a tab. not really what i want. let me know if my question is too confusing, and i'll try to add more code.

KabirKang-zz avatar Feb 23 '17 20:02 KabirKang-zz

I think I know what's the issue. I have one idea I'll try to send you tomorrow morning, maybe it works.

grabbou avatar Feb 23 '17 20:02 grabbou

ok thanks

KabirKang-zz avatar Feb 23 '17 21:02 KabirKang-zz

@grabbou does it involve redux?

KabirKang-zz avatar Feb 23 '17 21:02 KabirKang-zz

@KabirKang, to do this you have to have the Parent navigation as the StackNavigation, and then have your TabNavigation as one of the Stack screens. Then by turning on/off the visible of each of the top Stack screens you get your desired outcome.

import ContactView from './ContactView'; // Detail view of a contact
import TabNavigation from './TabNavigation'; // Child TabNavigation that would call navigate('Contact', {contactId: 1});
import { StackNavigator } from 'react-navigation';

const TopNavigation = StackNavigator({
  Home: {
    screen: TabNavigation
  },
  Contact: {
    screen: ContactView,
    navigationOptions: {
      header: {
        visible: true
      }
    }
  }
}, {
  navigationOptions: {
    header: {
      visible: false
    }
  }
});

export default TopNavigation;

dbhowell avatar Feb 23 '17 22:02 dbhowell

@dbhowell In your example you wrote header: {visible: true}. Did you mean tabBar? and would that work for tabbar?

KabirKang-zz avatar Feb 23 '17 22:02 KabirKang-zz

Note that { visible: true} in this case hides the Stack toolbar, not the Tabbar.

Edit: I meant that {visible true} shows the Stack toolbar

dbhowell avatar Feb 23 '17 22:02 dbhowell

@dbhowell So would the structure you suggest allow you to visit more screens within each tab or would each tab still need to have a nested stack navigator in each?

Also what does this do to the back button? If you were to switch tabs, and then click the Android back button, would it go back to the previous tab?

KabirKang-zz avatar Feb 23 '17 22:02 KabirKang-zz

It does get confusing, so the best thing is to try it out and see what the result is.

dbhowell avatar Feb 23 '17 23:02 dbhowell

@dbhowell that works fine with headers (if headerMode: "screen"), but that's probably because the header option is part of the stackNavigator.

tabBar: { visible: false } won't work since it's part of the parent TabNavigator.

I'm also interested in how to do this, as a typical use case for us would be to hide the tab bar on the second level navigation throughout the app.

Would be willing to help out implementing it, but need a few pointers on how to go about it. @grabbou, any ideas you have would be highly appreciated!

bjrn avatar Feb 28 '17 12:02 bjrn

@grabbou any update on this one?

ahmedlhanafy avatar Mar 01 '17 03:03 ahmedlhanafy

I think there should be an API which we can configure - This could update the global state of the navbar where it's configured - E.g inside a stack navigator:

static tabbarOptions = {
    hide: true,
    ...Otheroptions
}

However I'm not sure how react-navigation is setup internally.

jamesone avatar Mar 02 '17 22:03 jamesone

I faced this problem too today. I need to hide the tabBar in the second level of navigation in one tab.

I have a TabNavigator with StackNavigators in each tab. For one tab I'd like to hide the tabBar if I'm in in a deep screen of the StackNavigator.

Is it possible right now with a certain config? The idea is to "bubble up" the deep child screen navigationOptions config to the top TabNavigator parent.

Kerumen avatar Mar 04 '17 14:03 Kerumen

Create separate tab navigators with the config you want and use them.

satya164 avatar Mar 04 '17 15:03 satya164

Do you have an example? I can't figure out how to do this. Currently I have this config:

TabNavigator({
  Screen1:  { screen: StackNavigator({...}) }, 
  Screen2:  { screen: StackNavigator({...}) }, 
  Screen3:  { screen: StackNavigator({
    SubScreen1: { screen: ...  }, 
    SubScreen2: { screen: ...  }, 
  }) }, 
})

I want to hide the tabBar on the SubScreen2.

Kerumen avatar Mar 04 '17 16:03 Kerumen

Hi,

I found a simple solution.

if we take @Kerumen config, we can add a per tab navigationOptions (has I think you've already done) :

TabNavigator({
    Screen1: { screen: StackNavigator({ ...}) },
    Screen2: { screen: StackNavigator({ ...}) },
    Screen3: {
        screen: StackNavigator({
            SubScreen1: { screen: ...  },
            SubScreen2: { screen: ...  },
        }),
        navigationOptions: {
            title: 'Your title',
            tabBar: (state, acc) => {
                return {
                    label: '',
                    icon: ...,
                    visible: (acc && acc.visible !== 'undefined') ? acc.visible : true,
                }
            },
        }
    },
})

The main thing there is that the tabBar option is a function that take 2 parameters : state and acc, acc is the tabBar config from the current Screen (well it's a little more complex than that). You just have to merge that.

And for example in SubScreen2 you can have this :

static navigationOptions = {
    header: { ...},
    tabBar: {
        visible: false,
    },
}

Hope it helps ;-)

adogor avatar Mar 17 '17 13:03 adogor

Hi, I am still struggling with this. I've basically got a stackNavigator and want to make a tabNavigator a parent of the stackNavigator. The stackNavigator got 2 screens and I only want to show on one of those the navbar. This is what my code looks like:

` export const MainStack = StackNavigator({ Home: { screen: Home, navigationOptions: { title: "Home" } }, AdventureArticle: { screen: AdventureArticle, navigationOptions: { title: "Adventure" } } })

export const MainTabs = TabNavigator( { MainStack: { screen: MainStack, // Replaced Feed with FeedScreen navigationOptions: { tabBar: { label: 'Home', icon: ({ tintColor }) => <Icon name={"google-plus-square"} size={35} color={tintColor} />, showIcon: true }, }, }, }, { tabBarPosition: "bottom" } );`

I tried putting this in the code of my "AdventureArticle" screen (in my stackNavigator): static navigationOptions = { tabBar: () => ({ visible: false, label: "test" }), }; But as I read on this topic, it doesn't work.

Is there a simple and straightforward way of handling this?

bramvbilsen avatar Mar 18 '17 20:03 bramvbilsen