In this section, we will explore the Stack Navigator, a fundamental component of React Navigation that allows you to navigate between different screens in a stack-like manner. This is similar to how web pages work, where you can navigate back and forth between pages.
Key Concepts
- Stack Navigator: A navigator that allows you to transition between screens and manage navigation history.
- Screen: A component that represents a single view in your app.
- Navigation Prop: A prop automatically passed to each screen component, containing methods and properties to navigate between screens.
Setting Up Stack Navigator
Step 1: Install React Navigation
First, you need to install the necessary packages for React Navigation.
You also need to install the required dependencies:
Step 2: Set Up Navigation Container
Wrap your app in a NavigationContainer
. This component manages the navigation tree and contains the navigation state.
import * as React from 'react'; import { NavigationContainer } from '@react-navigation/native'; export default function App() { return ( <NavigationContainer> {/* Rest of your app code */} </NavigationContainer> ); }
Step 3: Create a Stack Navigator
Create a stack navigator using createStackNavigator
and define your screens.
import { createStackNavigator } from '@react-navigation/stack'; const Stack = createStackNavigator(); function HomeScreen({ navigation }) { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Home Screen</Text> <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> ); } export default function App() { return ( <NavigationContainer> <Stack.Navigator initialRouteName="Home"> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }
Explanation
- NavigationContainer: This component is necessary to manage the navigation state.
- createStackNavigator: This function creates a stack navigator.
- Stack.Navigator: This component is used to configure the navigator.
- Stack.Screen: This component defines each screen in the stack.
Step 4: Navigating Between Screens
Use the navigation
prop to navigate between screens. The navigate
method allows you to move to a different screen.
Practical Example
Let's create a simple app with two screens: Home and Details.
HomeScreen Component
import React from 'react'; import { View, Text, Button } from 'react-native'; function HomeScreen({ navigation }) { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Home Screen</Text> <Button title="Go to Details" onPress={() => navigation.navigate('Details')} /> </View> ); } export default HomeScreen;
DetailsScreen Component
import React from 'react'; import { View, Text } from 'react-native'; function DetailsScreen() { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Details Screen</Text> </View> ); } export default DetailsScreen;
App Component
import React from 'react'; import { NavigationContainer } from '@react-navigation/native'; import { createStackNavigator } from '@react-navigation/stack'; import HomeScreen from './HomeScreen'; import DetailsScreen from './DetailsScreen'; const Stack = createStackNavigator(); export default function App() { return ( <NavigationContainer> <Stack.Navigator initialRouteName="Home"> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }
Exercises
Exercise 1: Add a New Screen
- Create a new screen component called
ProfileScreen
. - Add
ProfileScreen
to the stack navigator. - Add a button in
HomeScreen
to navigate toProfileScreen
.
Solution
ProfileScreen Component
import React from 'react'; import { View, Text } from 'react-native'; function ProfileScreen() { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Profile Screen</Text> </View> ); } export default ProfileScreen;
Update App Component
import React from 'react'; import { NavigationContainer } from '@react-navigation/native'; import { createStackNavigator } from '@react-navigation/stack'; import HomeScreen from './HomeScreen'; import DetailsScreen from './DetailsScreen'; import ProfileScreen from './ProfileScreen'; const Stack = createStackNavigator(); export default function App() { return ( <NavigationContainer> <Stack.Navigator initialRouteName="Home"> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> <Stack.Screen name="Profile" component={ProfileScreen} /> </Stack.Navigator> </NavigationContainer> ); }
Update HomeScreen Component
import React from 'react'; import { View, Text, Button } from 'react-native'; function HomeScreen({ navigation }) { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Home Screen</Text> <Button title="Go to Details" onPress={() => navigation.navigate('Details')} /> <Button title="Go to Profile" onPress={() => navigation.navigate('Profile')} /> </View> ); } export default HomeScreen;
Conclusion
In this section, you learned how to set up and use the Stack Navigator in React Native. You now know how to navigate between different screens and manage navigation history. This is a fundamental skill for building multi-screen applications in React Native. In the next section, we will explore the Tab Navigator, which allows you to create a tab-based navigation system.
React Native Course
Module 1: Introduction to React Native
- What is React Native?
- Setting Up the Development Environment
- Hello World App
- Understanding JSX
- Components and Props
Module 2: Core Components and Styling
- Core Components Overview
- Text, View, and Image
- Styling with Flexbox
- Handling User Input
- ScrollView and ListView
Module 3: State and Lifecycle
- State and Lifecycle Methods
- Handling Events
- Conditional Rendering
- Lists and Keys
- Forms and Controlled Components
Module 4: Navigation
- Introduction to React Navigation
- Stack Navigator
- Tab Navigator
- Drawer Navigator
- Passing Parameters to Routes
Module 5: Networking and Data
- Fetching Data with Fetch API
- Using Axios for HTTP Requests
- Handling Network Errors
- AsyncStorage for Local Data
- Integrating with REST APIs
Module 6: Advanced Concepts
Module 7: Deployment and Publishing
- Building for iOS
- Building for Android
- Publishing to App Store
- Publishing to Google Play
- Continuous Integration and Delivery