• Articles
  • Tutorials
  • Interview Questions

React Native Navigation: Navigating Between Screens

React Native Navigation: Navigating Between Screens

React Native Navigation can help businesses save time and money while delivering top-notch user experiences. Plus, with the ability to target older versions of each platform, businesses can reach a wider audience and ensure that their apps are accessible to as many users as possible. It’s a win-win situation for everyone involved! Explore the ins and outs of React Native Navigation, backed by practical examples and step-by-step instructions, with this post.

Know the difference between React JS and React Native through this video:

Video Thumbnail

What is React Navigation?

React Navigation is a popular Javascript library that allows developers to add navigation and routing to their React Native applications. Navigation refers to the way users move between different screens or pages within an application, and React Navigation provides a set of tools and components to make this process easier.

React Navigation supports several types of navigation, including stack, tab, drawer, and switch navigation. Stack navigation allows users to move between screens in a “stack” or “history” of screens. Tab navigation displays different screens as tabs, allowing users to switch between them. Drawer navigation provides a “drawer” or “sidebar” menu that users can slide out to access different screens. Switch navigation allows for conditional rendering of screens based on certain criteria.

What is React Native Navigation?

React Native Navigation is a popular library that provides a seamless and efficient way to handle navigation between screens in a React Native application. Unlike React Navigation, which is primarily JavaScript-based, React Native Navigation leverages native platform components for smoother transitions and better performance.

One of the key advantages of React Native Navigation is its ability to create fluid navigation experiences that closely resemble native apps. It achieves this by utilizing native navigation controllers such as UINavigationController for iOS and Fragment for Android, ensuring a consistent and intuitive user experience across platforms.

How to Install Navigation in React Native?

To get started with React Native Navigation, we need to install and configure the necessary dependencies. Begin by installing the library using npm or Yarn:

npm install react-native-navigation

Once installed, we need to link the library to our project using the appropriate native package manager. For iOS, we can use Cocoapods, while for Android, we need to modify the Gradle files.

After linking, we need to initialize React Native Navigation in our app’s entry point. This involves registering screens and setting up a navigation stack. By defining the initial screen and navigation options, we can customize the appearance and behavior of our navigation stack.

Navigating Between Screens

Navigating between Screens

In React Native Navigation, navigating between screens is accomplished through the use of navigators. There are several types of navigators available, such as Stack Navigator, Tab Navigator, and Drawer Navigator. Each navigator has its own purpose and usage scenarios.

Example: Creating a Stack Navigator:

A Stack Navigation is commonly used for managing a stack of screens where each screen is placed on top of the previous one. Let’s consider an example where we have two screens: HomeScreen and DetailsScreen.

To establish a Stack Navigator, it is necessary to specify the screens and their corresponding components. The createStackNavigator function offered by React Native Navigation can be utilized for this purpose:

import { Navigation } from 'react-native-navigation';
const HomeScreen = (props) => {
  // Screen component implementation
};
const DetailsScreen = (props) => {
  // Screen component implementation
};
Navigation.registerComponent('Home', () => HomeScreen);
Navigation.registerComponent('Details', () => DetailsScreen);
Navigation.setRoot({
  root: {
    stack: {
      children: [
        {
          component: {
            name: 'Home',
          },
        },
      ],
    },
  },
});

In the example above, we register our screen components using Navigation.registerComponent. Then, we set the root of our navigation hierarchy using Navigation.setRoot, specifying the initial screen as ‘Home’. This creates a basic stack navigator with the HomeScreen as the initial screen.

Example: Implementing a Bottom Tab Navigator:

A Bottom Tab Navigation is useful for scenarios where we want to display multiple screens as tabs at the bottom of the screen. Each tab represents a separate screen that can be easily switched between.

To implement a Bottom Tab Navigator, we define our screens and their components, similar to the Stack Navigator example. We then use the createBottomTabNavigator function provided by React Native Navigation:

import { Navigation } from 'react-native-navigation';
const HomeScreen = (props) => {
  // Screen component implementation
};
const SettingsScreen = (props) => {
  // Screen component implementation
};
Navigation.registerComponent('Home', () => HomeScreen);
Navigation.registerComponent('Settings', () => SettingsScreen);
Navigation.setRoot({
  root: {
    bottomTabs: {
      children: [
        {
          stack: {
            children: [
              {
                component: {
                  name: 'Home',
                },
              },
            ],
          },
        },
        {
          component: {
            name: 'Settings',
          },
        },
      ],
    },
  },
});

In the example above, we register our screen components and set the root of our navigation hierarchy using Navigation.setRoot. The bottomTabs property specifies the bottom tab navigator, and each tab is defined as a stack or component object. This creates a bottom tab navigator with two tabs: Home and Settings.

Passing Data between Screens

Transferring data between screens is a frequent need in React Native Navigation. Luckily, React Native Navigation offers multiple ways to accomplish this task effortlessly. One of the simplest approaches is by utilizing parameters.

Example: Sending and Receiving Data with Params

Suppose we have two screens, ScreenA and ScreenB, and we wish to transfer data from ScreenA to ScreenB. We can achieve this by utilizing params, as demonstrated below:

In ScreenA:

import { useNavigation } from '@react-navigation/native';
const ScreenA = () => {
  const navigation = useNavigation();
  const handlePress = () => {
    navigation.navigate('ScreenB', { data: 'Hello from ScreenA' });
  };
  return (
    <View>
      <Button title="Go to ScreenB" onPress={handlePress} />
    </View>
  );
};
export default ScreenA;

In ScreenB:

import { useRoute } from '@react-navigation/native';
const ScreenB = () => {
  const route = useRoute();
  const { data } = route.params;
  return (
    <View>
      <Text>{data}</Text>
    </View>
  );
};
export default ScreenB;

In this example, we use the useNavigation hook in ScreenA to access the navigation object and call navigate to go to ScreenB. We pass the data as an object in the second parameter of the navigate function.

To obtain the transferred data in ScreenB, we can use the useRoute hook to access the current route and retrieve the data from the params object.

Modals and Overlays

Modals and overlays are commonly used in mobile apps to display additional information or actions without navigating to a new screen. React Native Navigation provides support for creating modals and overlays, giving developers flexibility in enhancing the user experience.

Example: Creating a Modal Overlay

Let’s create a simple example where we have a button in ScreenA, and when the button is pressed, a modal overlay appears.

In ScreenA:

import { useNavigation } from '@react-navigation/native';
const ScreenA = () => {
  const navigation = useNavigation();
  const handlePress = () => {
    navigation.navigate('ModalScreen');
  };
  return (
    <View>
      <Button title="Open Modal" onPress={handlePress} />
    </View>
  );
};
export default ScreenA;

In AppNavigator:

import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer, createModalNavigator } from 'react-navigation';
// Import your screens
import ScreenA from './ScreenA';
import ModalScreen from './ModalScreen';
const MainStack = createStackNavigator(
  {
    ScreenA: { screen: ScreenA },
  },
  {
    defaultNavigationOptions: {
      headerTitleAlign: 'center',
    },
  }
);
const RootStack = createModalNavigator(
  {
    MainStack: { screen: MainStack },
    ModalScreen: { screen: ModalScreen },
  },
  {
    mode: 'modal',
    headerMode: 'none',
  }
);
export default createAppContainer(RootStack);

In this example, we navigate to the ModalScreen when the button is pressed. We define a modal overlay by using the createModalNavigator function and specifying the mode as ‘modal’. The headerMode is set to ‘none’ to hide the header in the modal overlay.

Deep Linking

Deep linking allows users to navigate directly to a specific screen within an app using a URL or URI. React Native Navigation supports deep linking, enabling users to access specific screens and even pass parameters through the deep link.

Example: Handling Deep Links in React Native Navigation

To handle deep links in React Native Navigation, we need to configure our app to handle the incoming links and map them to the corresponding screens.

In AppNavigator:

import { Navigation } from 'react-native-navigation';
// Import your screens
import HomeScreen from './HomeScreen';
import ProductScreen from './ProductScreen';
const registerScreens = () => {
  Navigation.registerComponent('Home', () => HomeScreen);
  Navigation.registerComponent('Product', () => ProductScreen);
};
const startApp = () => {
  Navigation.setRoot({
    root: {
      stack: {
        children: [
          {
            component: {
              name: 'Home',
            },
          },
        ],
      },
    },
 });
};
registerScreens();
startApp();

In ProductScreen:

import { Navigation } from 'react-native-navigation';
const ProductScreen = ({ productId }) => {
  // Fetch product details using the productId
  return (
    // Display product details
  );
};
ProductScreen.options = {
  topBar: {
    title: {
      text: 'Product Details',
    },
  },
};
export default ProductScreen;

In this example, we register our screens using the Navigation.registerComponent function. We have a HomeScreen and a ProductScreen.

When a deep link like myapp://product/123 is triggered, we extract the productId from the link and pass it as a prop to the ProductScreen. The ProductScreen can then fetch the product details using the productId and display them.

Customizing Navigation Styles

React Native Navigation provides various options for customizing navigation styles, allowing developers to create unique and visually appealing navigation experiences.

Example: Customizing the Header Appearance

To customize the header appearance, we can use the navigationOptions object in our screen components.

In ScreenA:

import React from 'react';
import { View, Text } from 'react-native';
const ScreenA = () => {
  return (
    <View>
      <Text>Screen A</Text>
    </View>
  );
};
ScreenA.navigationOptions = {
  title: 'Custom Header',
  headerStyle: {
    backgroundColor: '#f4511e',
  },
  headerTintColor: '#fff',
  headerTitleStyle: {
    fontWeight: 'bold',
  },
};
export default ScreenA;

In this example, we set the title to ‘Custom Header’, headerStyle to have a background color of ‘#f4511e’, headerTintColor to ‘#fff’ for the text color, and headerTitleStyle to have a bold font weight.

Navigation Lifecycle Events

Navigation Lifecycle Events

React Native Navigation provides several lifecycle events that we can utilize to perform actions at specific points in the navigation flow.

Example: Handling Focus and Blur Events

We can handle the focus and blur events to perform actions when a screen comes into focus or loses focus.

In ScreenA:

import { useFocusEffect } from '@react-navigation/native';
const ScreenA = () => {
  useFocusEffect(
    React.useCallback(() => {
      // Perform actions when ScreenA comes into focus
      return () => {
        // Perform actions when ScreenA loses focus
      };
    }, [])
  );
  return (
    <View>
      {/* Screen content */}
    </View>
  );
};
export default ScreenA;

In this example, we use the useFocusEffect hook to handle the focus and blur events. We pass a callback function that will be executed when the screen comes into focus. We can perform any necessary actions within this callback.

Conclusion

The upcoming times of React Native Navigation are looking brighter than ever! With its growing popularity and active community, this native navigation library is sure to continue evolving and improving in the years to come. We can expect to see even more features and enhancements added, making it easier than ever for businesses and developers to create stunning, high-performance mobile apps. So, if you’re not already using React Native Navigation, now is the perfect time to jump on board and see what all the fuss is about!

Course Schedule

Name Date Details
Web Development Courses 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.