React Native Tutorial - 74 - Navigation between Screens

3 min read 4 hours ago
Published on Sep 13, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through implementing navigation between screens in a React Native application. It is essential for building mobile applications that require user interaction across multiple views. By the end of this tutorial, you'll understand how to set up navigation and manage screen transitions effectively.

Step 1: Install React Navigation

First, you need to install the React Navigation library, which is crucial for managing navigation in React Native apps.

  1. Open your terminal.

  2. Navigate to your React Native project directory.

  3. Run the following command to install the necessary packages:

    npm install @react-navigation/native @react-navigation/stack
    
  4. Additionally, install the required dependencies for React Navigation:

    npm install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context @react-native-community/masked-view
    
  5. For iOS, ensure you install the pods:

    cd ios
    pod install
    cd ..
    

Step 2: Set Up Navigation Container

Next, create a navigation container that will hold your navigation structure.

  1. Open your main application file, typically App.js.

  2. Import the necessary components from React Navigation:

    import 'react-native-gesture-handler';
    import React from 'react';
    import { NavigationContainer } from '@react-navigation/native';
    import { createStackNavigator } from '@react-navigation/stack';
    
  3. Initialize the stack navigator:

    const Stack = createStackNavigator();
    
  4. Wrap your application in a NavigationContainer:

    export default function App() {
      return (
        <NavigationContainer>
          <Stack.Navigator>
            {/* Screens will be added here */}
          </Stack.Navigator>
        </NavigationContainer>
      );
    }
    

Step 3: Create Screens

You will need to define the screens that you want to navigate between.

  1. Create two new JavaScript files for your screens, e.g., HomeScreen.js and DetailsScreen.js.

  2. In HomeScreen.js, define a simple component:

    import React from 'react';
    import { View, Text, Button } from 'react-native';
    
    const HomeScreen = ({ navigation }) => {
      return (
        <View>
          <Text>Home Screen</Text>
          <Button
            title="Go to Details"
            onPress={() => navigation.navigate('Details')}
          />
        </View>
      );
    };
    
    export default HomeScreen;
    
  3. In DetailsScreen.js, create another simple component:

    import React from 'react';
    import { View, Text, Button } from 'react-native';
    
    const DetailsScreen = ({ navigation }) => {
      return (
        <View>
          <Text>Details Screen</Text>
          <Button
            title="Go Back"
            onPress={() => navigation.goBack()}
          />
        </View>
      );
    };
    
    export default DetailsScreen;
    

Step 4: Register Screens in Navigator

Now, you need to register the screens in your stack navigator.

  1. Modify the Stack.Navigator in App.js to include your screens:

    import HomeScreen from './HomeScreen';
    import DetailsScreen from './DetailsScreen';
    
    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>
      );
    }
    

Conclusion

You have successfully set up navigation between screens in a React Native application. Key points to remember include:

  • Installing React Navigation and its dependencies.
  • Creating a navigation container to manage your app's screens.
  • Defining individual screen components and registering them in the stack navigator.

As a next step, you can explore additional features of React Navigation, such as passing parameters between screens, using different navigation types (like tab or drawer navigation), and customizing screen transitions. Happy coding!