0
0
React Nativemobile~20 mins

Nested navigators in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Navigator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
navigation
intermediate
2:00remaining
What screen is shown after navigating to 'Profile' inside nested stack?
Consider a React Navigation setup with a bottom tab navigator containing a nested stack navigator for the 'User' tab. The stack has screens: 'UserHome' and 'Profile'. If you navigate to 'Profile' from 'UserHome', which screen will be visible?
React Native
const UserStack = createStackNavigator();

function UserStackScreen() {
  return (
    <UserStack.Navigator>
      <UserStack.Screen name="UserHome" component={UserHomeScreen} />
      <UserStack.Screen name="Profile" component={ProfileScreen} />
    </UserStack.Navigator>
  );
}

const Tab = createBottomTabNavigator();

function App() {
  return (
    <NavigationContainer>
      <Tab.Navigator>
        <Tab.Screen name="Home" component={HomeScreen} />
        <Tab.Screen name="User" component={UserStackScreen} />
      </Tab.Navigator>
    </NavigationContainer>
  );
}

// From UserHomeScreen, calling navigation.navigate('Profile')
AAn error occurs because 'Profile' is not a tab screen.
BThe HomeScreen from the bottom tab navigator is shown.
CThe ProfileScreen inside the UserStack is shown.
DThe UserHomeScreen remains visible; navigation does nothing.
Attempts:
2 left
💡 Hint
Think about how nested stack navigators handle screen names inside their own navigator.
ui_behavior
intermediate
2:00remaining
How does header display behave with nested stack inside tab navigator?
In a React Navigation app, a bottom tab navigator contains a nested stack navigator. The stack navigator has header shown by default. What happens to the header when switching tabs?
React Native
const Stack = createStackNavigator();
const Tab = createBottomTabNavigator();

function StackScreen() {
  return (
    <Stack.Navigator>
      <Stack.Screen name="ScreenA" component={ScreenA} />
      <Stack.Screen name="ScreenB" component={ScreenB} />
    </Stack.Navigator>
  );
}

function App() {
  return (
    <NavigationContainer>
      <Tab.Navigator>
        <Tab.Screen name="StackTab" component={StackScreen} />
        <Tab.Screen name="OtherTab" component={OtherScreen} />
      </Tab.Navigator>
    </NavigationContainer>
  );
}
AHeaders from both tab and stack navigators are shown simultaneously.
BThe header from the stack navigator is visible only on the StackTab screens.
CNo header is shown because the tab navigator disables headers by default.
DThe header is always visible on all tabs regardless of nested navigators.
Attempts:
2 left
💡 Hint
Remember that stack navigators control their own headers independently inside tabs.
lifecycle
advanced
2:00remaining
Which lifecycle hook triggers when switching between nested navigators?
In React Navigation, when switching from one tab to another, each with nested stack navigators, which React hook is called on the screen component that becomes visible?
React Native
function ScreenA({ navigation }) {
  React.useEffect(() => {
    console.log('ScreenA mounted or focused');
  }, []);

  React.useFocusEffect(
    React.useCallback(() => {
      console.log('ScreenA focused');
      return () => console.log('ScreenA unfocused');
    }, [])
  );

  return <View><Text>Screen A</Text></View>;
}
AuseFocusEffect runs when the screen becomes visible after tab switch.
BNo lifecycle hooks run when switching tabs.
CcomponentDidMount is called again on tab switch.
DuseEffect with empty dependency runs every time the tab changes.
Attempts:
2 left
💡 Hint
Think about hooks that respond to screen focus in React Navigation.
📝 Syntax
advanced
2:00remaining
Which code correctly nests a stack navigator inside a tab navigator?
Choose the correct React Navigation code snippet that nests a stack navigator inside a bottom tab navigator.
A
const Stack = createStackNavigator();
const Tab = createBottomTabNavigator();

function StackScreen() {
  return (
    &lt;Stack.Navigator&gt;
      &lt;Stack.Screen name="Home" component={HomeScreen} /&gt;
    &lt;/Stack.Navigator&gt;
  );
}

function App() {
  return (
    &lt;NavigationContainer&gt;
      &lt;Tab.Navigator&gt;
        &lt;Tab.Screen name="Stack" component={StackScreen} /&gt;
        &lt;Tab.Screen name="Settings" component={SettingsScreen} /&gt;
      &lt;/Tab.Navigator&gt;
    &lt;/NavigationContainer&gt;
  );
}
B
const Tab = createBottomTabNavigator();
const Stack = createStackNavigator();

function App() {
  return (
    &lt;NavigationContainer&gt;
      &lt;Stack.Navigator&gt;
        &lt;Stack.Screen name="Tabs" component={Tab} /&gt;
      &lt;/Stack.Navigator&gt;
    &lt;/NavigationContainer&gt;
  );
}
C
const Tab = createBottomTabNavigator();

function App() {
  return (
    &lt;NavigationContainer&gt;
      &lt;Tab.Navigator&gt;
        &lt;Tab.Screen name="Home" component={HomeScreen} /&gt;
        &lt;Tab.Screen name="Stack" component={StackScreen} /&gt;
      &lt;/Tab.Navigator&gt;
    &lt;/NavigationContainer&gt;
  );
}

function StackScreen() {
  return (
    &lt;Stack.Navigator&gt;
      &lt;Stack.Screen name="Profile" component={ProfileScreen} /&gt;
    &lt;/Stack.Navigator&gt;
  );
}
D
const Stack = createStackNavigator();
const Tab = createBottomTabNavigator();

function App() {
  return (
    &lt;NavigationContainer&gt;
      &lt;Stack.Navigator&gt;
        &lt;Stack.Screen name="Home" component={HomeScreen} /&gt;
        &lt;Stack.Screen name="Tabs" component={Tab} /&gt;
      &lt;/Stack.Navigator&gt;
    &lt;/NavigationContainer&gt;
  );
}
Attempts:
2 left
💡 Hint
The stack navigator must be a component inside the tab navigator's screen.
🔧 Debug
expert
2:00remaining
Why does navigation.navigate('Profile') fail inside nested stack?
Given a nested stack navigator inside a tab navigator, calling navigation.navigate('Profile') from a screen inside the tab causes an error: 'The action 'NAVIGATE' with payload {name: 'Profile'} was not handled by any navigator.' What is the likely cause?
React Native
const Stack = createStackNavigator();
const Tab = createBottomTabNavigator();

function StackScreen() {
  return (
    <Stack.Navigator>
      <Stack.Screen name="Home" component={HomeScreen} />
      <Stack.Screen name="Profile" component={ProfileScreen} />
    </Stack.Navigator>
  );
}

function App() {
  return (
    <NavigationContainer>
      <Tab.Navigator>
        <Tab.Screen name="StackTab" component={StackScreen} />
        <Tab.Screen name="Settings" component={SettingsScreen} />
      </Tab.Navigator>
    </NavigationContainer>
  );
}

// From a screen outside StackTab, calling navigation.navigate('Profile')
AThe 'Profile' screen name is misspelled in the stack navigator.
BThe 'Profile' screen is not registered in any navigator, causing the error.
CThe navigation container is missing, so navigation actions fail.
DTrying to navigate to 'Profile' from outside the nested stack without specifying the nested navigator causes the error.
Attempts:
2 left
💡 Hint
Think about how navigation actions target nested navigators by name.