Navigating Between Screens

This guide covers the various navigation components available in React Native. If you are just getting started with navigation, you will probably want to use React Navigation. React Navigation provides an easy to use navigation solution, with the ability to present common stack navigation and tabbed navigation patterns on both iOS and Android.

If you'd like to achieve a native look and feel on both iOS and Android, or you're integrating React Native into an app that already manages navigation natively, the following library provides native navigation on both platforms: react-native-navigation.

React Navigation

Install the react-navigation package in your React Native project.

Install react-native-gesture-handler and react-navigation-stack

Creating a stack navigator

Moving between screens

Configuring

  • Setting the header title
  • Using params in the title
  • Passing params to screen

getParam - get a specific param with fallback.

  • Move back in the stack

goBack - close active screen and move back in the stack.

  • Custom Android back button behavior

The following code snippet demostrates the situation. We make use of BackHandler which comes with react-native and we subscribe to navigation lifecycle updates to add our custom hardwareBackPress listener.

  • Updating navigationOptions with setParams
  • Additional configuration

You can read the full list of available navigationOptions for screens inside of a stack navigator in the createStackNavigator reference.

StackNavigatorConfig

  • initialRouteName - Sets the default screen of the stack. Must match one of the keys in route configs.
  • headerMode - No header will be rendered.
  • gesturesEnabled - Whether you can use gestures to dismiss this screen. Defaults to true on iOS, false on Android.
  • transitionConfig - Function to return an object that is merged with the default screen transitions.

Example transitionConfig: View on Github react-navigation-transitions

View code Navigating Between Screens App.js

Tab navigation

This guide covers createBottomTabNavigator. You may also use createMaterialBottomTabNavigator and createMaterialTopTabNavigator to add tabs to your application.

  • createBottomTabNavigator
  • createMaterialBottomTabNavigator
  • createMaterialTopTabNavigator