------------------- | Home Screen | | | | [Go to Details] | | | -------------------
React Navigation installation in React Native - Mini App: Build & Ship
import React from 'react'; import { Button, Text, View } from 'react-native'; export default function HomeScreen({ navigation }) { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> {/* TODO: Add Button to navigate to Details screen */} </View> ); } export function DetailsScreen() { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Details Screen</Text> </View> ); } // TODO: Set up Navigation Container and Stack Navigator here
import React from 'react'; import { Button, Text, View } from 'react-native'; import { NavigationContainer } from '@react-navigation/native'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; function HomeScreen({ navigation }) { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Button title="Go to Details" onPress={() => navigation.navigate('Details')} /> </View> ); } function DetailsScreen() { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Details Screen</Text> </View> ); } const Stack = createNativeStackNavigator(); export default function App() { return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }
This solution shows how to install and set up React Navigation for a simple two-screen app.
First, you install the required packages:
npm install @react-navigation/native @react-navigation/native-stack react-native-screens react-native-safe-area-context react-native-gesture-handler react-native-reanimated
Then, you import NavigationContainer which manages navigation state, and createNativeStackNavigator to create a stack of screens.
The HomeScreen has a button that calls navigation.navigate('Details') to go to the Details screen.
The DetailsScreen just shows some text.
The App component wraps the stack navigator inside the navigation container.
This setup follows React Navigation's official guidelines and creates a working navigation flow.
------------------- | Home Screen | | | | [Go to Details] | | | ------------------- -- After tapping button -- ------------------- | Details Screen | | | | Details Screen | | | -------------------
Practice
What is the main purpose of React Navigation in a React Native app?
Solution
Step 1: Understand React Navigation's role
React Navigation is designed to help users switch between different screens or pages in a React Native app.Step 2: Compare with other options
Styling is done with stylesheets, state management uses other libraries, and data fetching is separate from navigation.Final Answer:
To move between different screens in the app -> Option CQuick Check:
Navigation = Moving between screens [OK]
- Confusing navigation with styling
- Mixing navigation with state management
- Thinking it fetches data
Which command correctly installs the core React Navigation library using npm?
?Solution
Step 1: Identify the official package name
The official core package for React Navigation is '@react-navigation/native'.Step 2: Check other options
'react-navigation' is outdated, 'navigation-react' and '@react-navigation/core' are incorrect package names.Final Answer:
npm install @react-navigation/native -> Option AQuick Check:
Core package = @react-navigation/native [OK]
- Using outdated package names
- Misspelling package names
- Installing wrong packages
After installing React Navigation core, you run npm install react-native-screens react-native-safe-area-context. What is the expected result?
Solution
Step 1: Understand the purpose of the packages
'react-native-screens' improves screen rendering performance, and 'react-native-safe-area-context' manages safe areas on devices.Step 2: Predict the effect on the app
Installing these packages enhances navigation experience by improving performance and layout safety.Final Answer:
The app will have improved screen performance and safe area handling -> Option DQuick Check:
Supporting packages improve navigation quality [OK]
- Thinking these packages cause crashes
- Assuming no effect
- Confusing with unrelated packages
You installed React Navigation and dependencies on iOS but forgot to run pod install. What issue will most likely occur?
Solution
Step 1: Understand the role of
On iOS,pod installpod installlinks native dependencies required by React Navigation.Step 2: Predict the consequence of skipping it
Without runningpod install, native modules are missing, causing build failures or runtime crashes on iOS.Final Answer:
The app will fail to build or crash due to missing native modules -> Option BQuick Check:
Missing pod install = build failure [OK]
- Skipping pod install on iOS
- Assuming Android needs pod install
- Ignoring build errors
You want to install React Navigation in a new Expo project. Which sequence of commands is correct?
?Solution
Step 1: Identify Expo-specific installation
Expo recommends usingexpo installto install compatible versions of React Navigation and dependencies.Step 2: Check the correct packages
The core package is '@react-navigation/native' plus 'react-native-screens' and 'react-native-safe-area-context' for performance and layout.Step 3: Verify other options
Options B and C use outdated or incomplete commands; D uses npm but misses safe-area-context and pod install is not needed in Expo.Final Answer:
expo install @react-navigation/native react-native-screens react-native-safe-area-context -> Option AQuick Check:
Expo uses expo install with core packages [OK]
- Using npm instead of expo install in Expo
- Missing dependencies
- Running pod install in Expo projects
Ready to prove it?
Pass the test and this pattern moves unknown
