question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React Navigation `material-bottom-tabs` breaks nested Stack Navigators

See original GitHub issue

Current behaviour

On Android, the tab content is not visible the second time the tab is viewed. This is when using @react-navigation/material-bottom-tabs, with a Stack Navigator nested within one of the tabs.

To reproduce:

  1. Press the tab that contains the Stack Navigator. (The content will be visible at this point)
  2. Press a different tab.
  3. Press the first tab again, and nothing will be displayed.

The React Navigation docs said I should open an issue on this repo, and I believe the bug is related to @react-navigation/material-bottom-tabs. Let me know if is not the correct repo for this issue.

Expected behaviour

The content should always be displayed for the selected tab.

Code sample

import React from "react";
import { Button, Text, View, StyleSheet } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import { createMaterialBottomTabNavigator } from "@react-navigation/material-bottom-tabs";
import { createNativeStackNavigator } from "@react-navigation/native-stack";

const Tab = createMaterialBottomTabNavigator();
const Stack = createNativeStackNavigator();

// Top level "Bottom Tab" screens
function FirstTabScreen() {
  // THIS IS THE TAB THAT DOESN'T DISPLAY CORRECTLY
  return (
    <Stack.Navigator initialRouteName="StackOne">
      <Stack.Screen name="StackOne" component={StackScreenOne} />
      <Stack.Screen name="StackTwo" component={StackScreenTwo} />
    </Stack.Navigator>
  );
}
function SecondTabScreen() {
  return (
    <View style={styles.screen}>
      <Text>Second Tab!</Text>
    </View>
  );
}

// Inner "Stack" screens
function StackScreenOne({ navigation }) {
  return (
    <View style={styles.screen}>
      <Text>Stack One!</Text>
      <Button
        title="Go to Stack Screen Two"
        onPress={() => {
          navigation.navigate("StackTwo");
        }}
      />
    </View>
  );
}
function StackScreenTwo({ navigation }) {
  return (
    <View style={styles.screen}>
      <Text>Stack Two!</Text>
      <Button
        title="Go to Stack Screen One"
        onPress={() => {
          navigation.navigate("StackOne");
        }}
      />
    </View>
  );
}

export default function App() {
  return (
    <NavigationContainer>
      <Tab.Navigator initialRouteName="FirstTab">
        <Tab.Screen
          name="FirstTab"
          component={FirstTabScreen}
          options={{
            tabBarLabel: "FirstTab",
            tabBarIcon: "home-outline",
          }}
        />
        <Tab.Screen
          name="SecondTab"
          component={SecondTabScreen}
          options={{
            tabBarLabel: "SecondTab",
            tabBarIcon: "bell-outline",
          }}
        />
      </Tab.Navigator>
    </NavigationContainer>
  );
}

const styles = StyleSheet.create({
  screen: { flex: 1, justifyContent: "center", alignItems: "center" },
});

Screenshots (if applicable)

N/A

What have you tried

  • I tested on iOS and the bug is not present.
  • I downgraded to version 5.3.19 and the bug was no longer present.
  • I found an old stackoverflow question that sounds similar, so maybe the same bug has returned?
    • I tried setting a height/width on the “stack” screens using screenOptions on the Stack.Navigator, but it had no effect.

Your Environment

software version
android 12 (real device, I have not tested on an emulator)
react-native 0.64.3
react-native-paper 4.10.1
node 14.18.2
npm or yarn yarn
expo sdk 43.0.2

Snacky Snack

https://snack.expo.dev/AWE08jYKr

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
traviswimercommented, Dec 30, 2021

@amirfrd if you haven’t figured it out yet, this bug has apparently been fixed if you upgrade to react-native >0.65.0 (I haven’t personally tested this yet though)

There is also a simple fix if you don’t want to upgrade: https://github.com/software-mansion/react-native-screens/issues/1197#issuecomment-993682256

Basically just wrap your stack navigator with <View style={{flex: 1}} collapsable={false}></View>

3reactions
github-actions[bot]commented, Dec 7, 2021

Couldn’t find version numbers for the following packages in the issue:

  • react-native-vector-icons
  • npm
  • yarn
  • expo

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

The versions mentioned in the issue for the following packages differ from the latest versions on npm:

  • react-native (found: 0.64.3, latest: 0.66.3)

Can you verify that the issue still exists after upgrading to the latest versions of these packages?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nesting navigators - React Navigation
Nesting navigators means rendering a navigator inside a screen of another navigator, ... So here, a tab navigator is nested inside a stack...
Read more >
Stack nested inside Material Bottom Tab dont show the ...
When I navigate to the Stack inside the Material Bottom the first time, all works fine, after that you change tab and when...
Read more >
How To Add An Icon To Bottom Tab Bar With Nested Stack ...
Reset the state of any nested navigators when switching away from a screen. ... Getting Started with Stack Navigator using reactnavigation 5 in...
Read more >
react native navigation nested Code Example
Navigator >. 19. <Stack.Screen name="Home" component={HomeTabs} />. 20. <Stack. ... Answers related to “react native navigation nested”.
Read more >
Why you should not use useNavigation - Bam Tech
Deep dive in React Navigation typing with typescript. ... A screen in a nested stack navigator has to be changed to a root...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found