Challenge - 5 Problems
Nested Navigator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
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')Attempts:
2 left
💡 Hint
Think about how nested stack navigators handle screen names inside their own navigator.
✗ Incorrect
Navigating to 'Profile' inside the nested UserStack navigator shows the ProfileScreen because 'Profile' is defined in the stack navigator inside the 'User' tab. The bottom tab navigator does not have a screen named 'Profile', so navigation affects the nested stack.
❓ ui_behavior
intermediate2: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>
);
}Attempts:
2 left
💡 Hint
Remember that stack navigators control their own headers independently inside tabs.
✗ Incorrect
The stack navigator inside the tab controls its own header visibility. When on the StackTab, the stack header shows. On other tabs, the stack header is not visible because those tabs render different components.
❓ lifecycle
advanced2: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>;
}Attempts:
2 left
💡 Hint
Think about hooks that respond to screen focus in React Navigation.
✗ Incorrect
useFocusEffect runs its effect callback every time the screen gains focus, such as when switching tabs. useEffect with empty dependencies runs only once on mount. componentDidMount is not used in functional components.
📝 Syntax
advanced2: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.
Attempts:
2 left
💡 Hint
The stack navigator must be a component inside the tab navigator's screen.
✗ Incorrect
Option A correctly defines a stack navigator component and uses it as a screen inside the tab navigator. Option A incorrectly nests tabs inside a stack without defining the Tab component properly. Option A uses StackScreen before defining Stack, causing an error. Option A nests tabs inside a stack, which is not nesting stack inside tab.
🔧 Debug
expert2: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')Attempts:
2 left
💡 Hint
Think about how navigation actions target nested navigators by name.
✗ Incorrect
When navigating to a screen inside a nested navigator from outside, you must specify the nested navigator's name and the screen name, e.g., navigation.navigate('StackTab', { screen: 'Profile' }). Calling navigation.navigate('Profile') alone fails because the root navigator does not have a 'Profile' screen.