Achieving a bottom tab bar display exclusively on default screens within nested stacks in react-navigation

Suppose I have a Tab Navigator where each tab contains nested stack navigators (refer to the code snippet below). My goal is to have the tab bar visible only on the default screen of each nested stack, similar to how the current Twitter Android app operates.

Although the documentation provides a method for hiding the tab bar in screens using tab bar hiding, I found it not suitable for my specific scenario. The V5 documentation suggests an alternative approach involving the tabBarVisible option, but advises against using it.

const Tab = createBottomTabNavigator()
const HomeStack = createStackNavigator()
const ProfileStack = createStackNavigator()

const HomeStackScreens = () => (
  <HomeStack.Navigator initialRouteName="DefaultHomeScreen">
    <HomeStack.Screen name="DefaultHomeScreen" component={DefaultHomeScreen} /> {/* Show tab only here (default) */}
    <HomeStack.Screen name="OtherHomeScreen" component={OtherHomeScreen} /> {/* Hide tab here */}
  </HomeStack.Navigator>
)
const ProfileStackScreens = () => (
  <ProfileStack.Navigator initialRouteName="DefaultProfileScreen">
    <ProfileStack.Screen name="DefaultProfileScreen" component={DefaultProfileScreen} /> {/* Show tab only here (default) */}
    <ProfileStack.Screen name="OtherProfileScreen" component={OtherProfileScreen} /> {/* Hide tab here */}
  </ProfileStack.Navigator>
)

const App = () => (
  <NavigationContainer>
    <Tab.Navigator initialRouteName="Home">
      <Tab.Screen name="Home" component={HomeStackScreens } />
      <Tab.Screen name="Profile" component={ProfileStackScreens} />
    </Tab.Navigator>
  </NavigationContainer>
)

How can I adjust the navigators and screens to achieve this desired behavior for my particular use case?

P.S. I am currently using React Navigation version v5, but considering migrating to v6. Both versions are acceptable solutions.

Answer №1

changing the way navigation is structured.

considering a more versatile approach to hiding tab bars

const MainStack = createStackNavigator()
const Tab = createBottomTabNavigator()
const HomeStack = createStackNavigator()
const ProfileStack = createStackNavigator()

const HomeStackScreens = () => (
  <HomeStack.Navigator initialRouteName="DefaultHomeScreen">
    <HomeStack.Screen name="DefaultHomeScreen" component={DefaultHomeScreen} /> 
    {/*add additional screens with bottom tabs here*/}
  </HomeStack.Navigator>
)

const ProfileStackScreens = () => (
  <ProfileStack.Navigator initialRouteName="DefaultProfileScreen">
    <ProfileStack.Screen name="DefaultProfileScreen" component={DefaultProfileScreen} />
    {/*add additional screens with bottom tabs here*/}
  </ProfileStack.Navigator>
)

const TabsNavigator = () => (
   <Tab.Navigator initialRouteName="Home">
      <Tab.Screen name="Home" component={HomeStackScreens } />
      <Tab.Screen name="Profile" component={ProfileStackScreens} />
    </Tab.Navigator>
)


const App = () => (
  <NavigationContainer>
    <MainStack.Navigator>
      <MainStack.Screen name="tabsNavigator" component={TabsNavigator} />
      <MainStack.Screen name="OtherHomeScreen" component={OtherHomeScreen} />
      <MainStack.Screen name="OtherProfileScreen" component={OtherProfileScreen} />
      {/*add more screens without bottom tabs here*/}
    </MainStack.Navigator>
  </NavigationContainer>
)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What methods can be used to accurately display the data type with TypeOf()?

When working with the following code: const data = Observable.from([{name: 'Alice', age: 25}, {name: 'Bob', age: 35}]); console.log(typeof(data)); The type is displayed as Object(). Is there a way to obtain more specific information? ...

using a practical example of CORS in JavaScript

I attempted a cross-origin AJAX request (CORS) and stumbled upon this helpful example by MJHALL to get me started. Executing the example on our internal networks (2 separate domains) resulted in success, returning the desired outcome. However, when I inte ...

What could be the reason for the malfunctioning dropdown menu in my navigation bar upon clicking it?

After spending hours practicing creating a navbar using Bootstrap 3.3.7, I've hit a roadblock - the dropdown is not working when clicked on. It's frustrating because I have double-checked all my scripts and ensured that I have the latest version ...

Refresh tab controllers in Angular JS on every click event

Is there a way to refresh the tab controller every time a tab is clicked? Here's the current code: $scope.tabs = [ { id: 'tab1', title: 'tab1', icon: 'comments', templateUrl: 'tab1/tab1.tpl.html&ap ...

The Google Apps spreadsheet script occasionally fails to complete iteration but functions properly in all other aspects

I have a script that goes through a spreadsheet of student data one row at a time to email students and their parents if the student's grade is below 60. The spreadsheet columns include: Student ID, Name, Email Address, Parent's Email Address, an ...

Setting up a project with Angular 2 and NodeJS

Hello there, I have some inquiries about organizing the project structure of a MEAN application with Angular2. Initially, I followed the introductory guide on angular.io to create a basic Angular2 app. Now, I am attempting to incorporate this app into a N ...

Initiate node.js execution upon system boot

Just diving into node.js. In the process of setting up integration tests for a node.js app using mocha, I found this helpful guide: Here's how I created a server: var http = require('http'); this.server = http.createServer(function (req, ...

Setting default parameters for TypeScript generics

Let's say I define a function like this: const myFunc = <T, > (data: T) => { return data?.map((d) => ({name: d.name}) } The TypeScript compiler throws an error saying: Property 'name' does not exist on type 'T', whic ...

Issue with AngularJS filter not functioning properly

I have a specific situation where I am using an ng-repeat directive in the following manner: {"ng-repeat" => "city in cities() | filter: search"} In this context, each city object is structured like so: { attributes: {name: 'Boston'} } Furt ...

The scrolling feature induces a contraction to the left side

Recently, I implemented the code below to fix my menu when scrolling the page: var num = 5; $(window).bind('scroll', function () { if ($(window).scrollTop() > num) { $('.scroll').css({'position':'fixed&apo ...

Vue JS encountering issues in rendering method's returned data on DOM

HTML Template <div class="item" v-for="n, index in teamRoster"> <span> {{ getFantasyScore(n.personId) }} </span> </div> Function getFantasyScore(playerId) { if(playerId) { axios.get(config.NBLAPI + config.API.PLAYE ...

After integrating Redux into React, the map(item) function is unable to send "item" as props to a JSX component

Currently, I am working on integrating Redux into my React application. As part of this process, I have developed four actions to manage the "items" in my application. The initial three actions, namely GET_ITEMS, DELETE_ITEM, and ADD_ITEM, function seamles ...

Conceal object after a brief pause

Why does the element hide immediately instead of slowly fading out? I can't figure out why it doesn't first display the "P" tag and then gradually hide it. Please help me solve this issue. var step = 0.1; var delay = 90000; var displayMe = fun ...

Concentrate on the disappeared div element following the AJAX request

Encountering an unusual issue here that has me a bit puzzled. In one of my DIV elements, I have included several links which trigger an AJAX call (using jQuery) upon being clicked. The server (Controller) then responds with HTML content, specifically a JS ...

Move the menu button to the right of the title slide, beyond the edge of the card

Having an issue with the MuiCardHeader component. <CardHeader disableTypography avatar={renderAvatar()} action={ <IconButton onClick={toggleMenu}> <img src={MoreIcon} alt=""/> </IconButton ...

Using ng-options with the Add New feature is not compatible with the select statement

Hello everyone, I have some JSON values that look like this: [{"Employee":{"Emp_id":"EF00001","First_name":"Aruna","Last_name":"Jayalath"}}] Unfortunately, I am having trouble retrieving the values within the select statement when running this function i ...

Using jQuery and Ajax to dynamically populate a select input with items upon page initialization

I am facing an issue with 2 <select> inputs: <select id="country"> <option value="" selected>Choose</option> <option value="usa">USA</option> <option value="uk">UK</option> </select> <select ...

Having trouble getting a form to submit to a Rails server using AJAX in IE11 with jQuery

Currently, I'm attempting to transfer data from a form to a Rails server using AJAX. The form consists of two text inputs and one file input. Below is the code for my submit event handler: $("form").on("submit", function(event) { event.preventDefa ...

Tips for integrating ons-sliding-menu into ons-navigator using Angular ui-router?

I'm having trouble loading ons-sliding-menu inside the ons-navigator using angular ui-router. Can someone please take a look at my code below and let me know what I'm doing wrong? I'm not getting any errors in the console, but it's just ...

Enhance Your Charts: Learn how to dynamically adjust zoom levels between two timestamps with just a simple form submission

I have a Zingchart Area Graph that displays events on a timeline with time on the X-Axis. I want to be able to automatically zoom into a specific timeframe on the graph based on user input from a form. For example, if a user selects a start time of 8 AM an ...