Bird
Raised Fist0
React Nativemobile~20 mins

Nested navigators in React Native - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using nested navigators in a React Native app?
easy
A. To speed up the app's loading time
B. To reduce the app's file size
C. To organize screens into groups for easier navigation management
D. To disable navigation gestures

Solution

  1. Step 1: Understand navigator roles

    Nested navigators group related screens inside one navigator to keep navigation organized.
  2. Step 2: Identify main benefit

    This grouping makes managing navigation easier and improves user experience.
  3. Final Answer:

    To organize screens into groups for easier navigation management -> Option C
  4. Quick Check:

    Nested navigators = organize screens [OK]
Hint: Think of nested navigators as folders for screens [OK]
Common Mistakes:
  • Confusing nested navigators with performance optimization
  • Thinking nested navigators reduce app size
  • Assuming nested navigators disable gestures
2. Which of the following is the correct way to include a nested navigator inside a parent navigator in React Native?
easy
A. Wrap the nested navigator inside a View component without adding to parent
B. Use the nested navigator as a screen component inside the parent navigator
C. Call the nested navigator inside a useEffect hook
D. Import the nested navigator but do not add it to the parent navigator

Solution

  1. Step 1: Understand navigator nesting

    To nest navigators, you add the nested navigator as a screen inside the parent navigator.
  2. Step 2: Check other options

    The other options do not properly register the nested navigator for navigation.
  3. Final Answer:

    Use the nested navigator as a screen component inside the parent navigator -> Option B
  4. Quick Check:

    Nested navigator = screen in parent [OK]
Hint: Add nested navigator as a screen, not just import it [OK]
Common Mistakes:
  • Importing nested navigator but forgetting to add as screen
  • Trying to call navigator inside hooks
  • Wrapping navigator in View without registering
3. Given this nested navigator setup, what screen will be shown when navigating to 'Profile' from the root navigator?
const RootStack = createStackNavigator();
const HomeStack = createStackNavigator();

function HomeStackScreen() {
  return (
    <HomeStack.Navigator>
      <HomeStack.Screen name="Feed" component={FeedScreen} />
      <HomeStack.Screen name="Profile" component={ProfileScreen} />
    </HomeStack.Navigator>
  );
}

function App() {
  return (
    <NavigationContainer>
      <RootStack.Navigator>
        <RootStack.Screen name="Home" component={HomeStackScreen} />
        <RootStack.Screen name="Settings" component={SettingsScreen} />
      </RootStack.Navigator>
    </NavigationContainer>
  );
}
medium
A. The ProfileScreen inside the HomeStack navigator
B. The SettingsScreen from the RootStack navigator
C. An error because Profile is not a direct screen of RootStack
D. The FeedScreen inside the HomeStack navigator

Solution

  1. Step 1: Identify where 'Profile' screen is defined

    'Profile' is a screen inside the HomeStack nested navigator, not directly in RootStack.
  2. Step 2: Understand navigation to nested screens

    To navigate to 'Profile', you navigate through 'Home' stack, which contains 'Profile'. So 'ProfileScreen' will show.
  3. Final Answer:

    The ProfileScreen inside the HomeStack navigator -> Option A
  4. Quick Check:

    Nested screen 'Profile' = HomeStack.ProfileScreen [OK]
Hint: Nested screens show inside their own navigator [OK]
Common Mistakes:
  • Assuming nested screens are direct children of root navigator
  • Confusing SettingsScreen with ProfileScreen
  • Expecting error when navigating nested screens
4. What is wrong with this nested navigator code snippet?
const RootStack = createStackNavigator();
const Tab = createBottomTabNavigator();

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

function App() {
  return (
    <NavigationContainer>
      <RootStack.Navigator>
        <RootStack.Screen name="Tabs" component={TabNavigator} />
        <RootStack.Screen name="Settings" component={SettingsScreen} />
      </RootStack.Navigator>
    </NavigationContainer>
  );
}

// Navigation call:
navigation.navigate('Profile');
medium
A. NavigationContainer is missing
B. TabNavigator should be inside SettingsScreen
C. RootStack.Navigator cannot have TabNavigator as a screen
D. Cannot navigate directly to 'Profile' because it's inside the nested TabNavigator

Solution

  1. Step 1: Analyze navigation call

    Calling navigation.navigate('Profile') tries to navigate directly in RootStack, but 'Profile' is inside TabNavigator nested navigator.
  2. Step 2: Understand nested navigation rules

    You must navigate to 'Tabs' first, then inside TabNavigator navigate to 'Profile'. Direct navigation to nested screens by name won't work.
  3. Final Answer:

    Cannot navigate directly to 'Profile' because it's inside the nested TabNavigator -> Option D
  4. Quick Check:

    Nested screen navigation requires parent route [OK]
Hint: Navigate nested screens via parent navigator first [OK]
Common Mistakes:
  • Trying to navigate nested screen directly by name
  • Placing TabNavigator inside unrelated screen
  • Forgetting NavigationContainer
5. You want to create a nested navigator structure where the root navigator is a drawer, and inside one drawer screen you want a stack navigator with multiple screens. Which of the following code snippets correctly nests the stack navigator inside the drawer navigator?
hard
A. Add the stack navigator as a screen component inside the drawer navigator's screens
B. Wrap the drawer navigator inside the stack navigator component
C. Use two separate NavigationContainers for drawer and stack navigators
D. Place the stack navigator outside the NavigationContainer

Solution

  1. Step 1: Understand nesting structure

    The drawer navigator is the root, so the stack navigator must be a screen inside the drawer to nest properly.
  2. Step 2: Evaluate options

    Add the stack navigator as a screen component inside the drawer navigator's screens correctly nests stack inside drawer by adding it as a screen. The other options break navigation rules or cause errors.
  3. Final Answer:

    Add the stack navigator as a screen component inside the drawer navigator's screens -> Option A
  4. Quick Check:

    Nested navigator = screen inside parent navigator [OK]
Hint: Put nested navigator as a screen inside parent navigator [OK]
Common Mistakes:
  • Wrapping drawer inside stack instead of nesting stack inside drawer
  • Using multiple NavigationContainers
  • Placing navigators outside NavigationContainer
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard