React Navigation Setup
Learn to set up React Navigation library and configure the basic navigation structure for React Native applications. This is a foundational concept in cross-platform mobile development that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world React Native experience. Take your time with each section and practice the examples
Installing React Navigation
React Navigation is the most popular navigation library for React Native. Install it using npm: `npm install @react-navigation/native @react-navigation/stack @react-navigation/bottom-tabs @react-navigation/drawer`
Required Dependencies
- @react-navigation/native - Core navigation library
- @react-navigation/stack - Stack navigator — a critical concept in cross-platform mobile development that you will use frequently in real projects
- @react-navigation/bottom-tabs - Tab navigator — a critical concept in cross-platform mobile development that you will use frequently in real projects
- @react-navigation/drawer - Drawer navigator — a critical concept in cross-platform mobile development that you will use frequently in real projects
- react-native-screens - Native screen optimization — a critical concept in cross-platform mobile development that you will use frequently in real projects
- react-native-safe-area-context - Safe area handling
- react-native-gesture-handler - Gesture handling — a critical concept in cross-platform mobile development that you will use frequently in real projects
Basic Setup
Wrap your app with NavigationContainer and create your first navigator. The NavigationContainer manages the navigation state and provides navigation context to all child components.. This is an essential concept that every React Native developer must understand thoroughly. In professional development environments, getting this right can mean the difference between code that works reliably and code that breaks in production. The following sections break this down into clear, digestible pieces with practical examples you can try immediately
Basic Navigation Setup
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { View, Text, StyleSheet } from 'react-native';
// Create stack navigator
const Stack = createStackNavigator();
const Tab = createBottomTabNavigator();
// Screen components
const HomeScreen = ({ navigation }) => (
<View style={styles.container}>
<Text style={styles.title}>Home Screen</Text>
<Text style={styles.subtitle}>Welcome to React Navigation!</Text>
</View>
);
const ProfileScreen = ({ navigation }) => (
<View style={styles.container}>
<Text style={styles.title}>Profile Screen</Text>
<Text style={styles.subtitle}>User profile information</Text>
</View>
);
const SettingsScreen = ({ navigation }) => (
<View style={styles.container}>
<Text style={styles.title}>Settings Screen</Text>
<Text style={styles.subtitle}>App settings and preferences</Text>
</View>
);
// Tab Navigator
const TabNavigator = () => (
<Tab.Navigator
screenOptions={{
tabBarActiveTintColor: '#2196F3',
tabBarInactiveTintColor: 'gray',
}}
>
<Tab.Screen
name="Home"
component={HomeScreen}
options={{ title: 'Home' }}
/>
<Tab.Screen
name="Profile"
component={ProfileScreen}
options={{ title: 'Profile' }}
/>
<Tab.Screen
name="Settings"
component={SettingsScreen}
options={{ title: 'Settings' }}
/>
</Tab.Navigator>
);
// Main App with Stack Navigator
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator
screenOptions={{
headerStyle: {
backgroundColor: '#2196F3',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
}}
>
<Stack.Screen
name="Main"
component={TabNavigator}
options={{ headerShown: false }}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 20,
backgroundColor: '#f5f5f5',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 10,
color: '#333',
},
subtitle: {
fontSize: 16,
color: '#666',
textAlign: 'center',
},
});
export default App;