Skip to main content

React Native Moving Between Screens

In this section, we will discuss how to navigate from one route screen to another route screen and come back to the initial route. In the previous part of Navigation, we created the stack navigator with two route screens (Home and Profile).
Moving from one screen to another is performed by using the navigation prop, which passes down our screen components. It is similar to write the below code for a web browser:

  1. <a href="profiles.html">Go to Profile</a>  

The other way to write this would be:


  1. <a onClick={() => { document.location.href = 
    "profile.html"; }}>Go to Profile</a>  

Navigate to the new screen

Navigation from one screen to another screen is performed in different ways:
  1. <Button  
  2.           title="Go to URL"  
  3.           onPress={() => this.props.navigation.navigate('url')}  
  4.         />  

App.js

Add a Button component in 'HomeScreen' and perform an onPress{} action which calls the this.props.navigation.navigate('Profile') function. Clicking the Button component moves screen to 'ProfileScreen' layout.

  1. import React from 'react';  
  2. import { View, Text, Button } from 'react-native';  
  3. import { createStackNavigator, createAppContainer } from 'react-navigation';  
  4.   
  5. class HomeScreen extends React.Component {  
  6.     render() {  
  7.         return (  
  8.             <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>  
  9.                 <Text>Home Screen</Text>  
  10.                 <Button  
  11.                     title="Go to Profile"  
  12.                     onPress={() => this.props.navigation.navigate('Profile')}  
  13.                 />  
  14.             </View>  
  15.         );  
  16.     }  
  17. }  
  18. class ProfileScreen extends React.Component {  
  19.     render() {  
  20.         return (  
  21.             <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>  
  22.                 <Text>Profile Screen</Text>  
  23.             </View>  
  24.     );  
  25.     }  
  26. }  
  27.   
  28. const AppNavigator = createStackNavigator(  
  29.     {  
  30.         Home: HomeScreen,  
  31.         Profile: ProfileScreen  
  32.     },  
  33.     {  
  34.         initialRouteName: "Home"  
  35.     }  
  36. );  
  37.   
  38. const AppContainer = createAppContainer(AppNavigator);  
  39. export default class App extends React.Component {  
  40.     render() {  
  41.         return <AppContainer />;  
  42.     }  
  43. }  

Output:

React Native Moving Between Screens React Native Moving Between Screens
  • this.props.navigation: The navigation prop is passed the every screen component in stack navigation.
  • navigate('Profile'): Call the navigate function with the route name where we want to move.

Navigate to a route screen multiple times

Adding navigation from 'ProfileScreen' to 'Profile' URL doesn't make any change because we are already at Profile route.

  1. class ProfileScreen extends React.Component {  
  2.     render() {  
  3.         return (  
  4.             <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>  
  5.                 <Text>Profile Screen</Text>  
  6.                    <Button  
  7.                      title="Go to Profile"  
  8.                      onPress={() => this.props.navigation.navigate('Profile')}  
  9.                    />  
  10.              </View>  
  11.     );  
  12.     }  
  13. }  

To call the profiles screen, mainly in the case of passing unique data (params) to each route. To do this, we change navigate to push. The navigate push expresses the intent to add another route disregarding the existing navigation history.

  1. <Button  
  2.         title="Go to Profile"  
  3.          onPress={() => this.props.navigation.push('Profile')}  
  4. />  
On pressing the button call push method each time and add a new route to the navigation stack.

Going back

The header of stack navigator automatically includes a back button when there is a possibility to go back from the current screen. The single screen stack navigation doesn't provide the back button as there is nothing where we can go back.
Sometimes, we programmatically implement the back behavior, for that we can call this.props.navigation.goBack(); function.

App.js

  1. import React from 'react';  
  2. import { View, Text, Button } from 'react-native';  
  3. import { createStackNavigator, createAppContainer } from 'react-navigation';  
  4.   
  5. class HomeScreen extends React.Component {  
  6.     render() {  
  7.         return (  
  8.             <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>  
  9.                 <Text>Home Screen</Text>  
  10.                 <Button  
  11.                     title="Go to Profile"  
  12.                     onPress={() => this.props.navigation.push('Profile')}  
  13.                 />  
  14.             </View>  
  15.         );  
  16.     }  
  17. }  
  18. class ProfileScreen extends React.Component {  
  19.     render() {  
  20.         return (  
  21.             <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>  
  22.                 <Text>Profile Screen</Text>  
  23.                 <Button  
  24.                     title="Go to Profile... again"  
  25.                     onPress={() => this.props.navigation.push('Profile')}  
  26.                 />  
  27.                 <Button  
  28.                     title="Go to Home"  
  29.                     onPress={() => this.props.navigation.navigate('Home')}  
  30.                  />  
  31.                 <Button  
  32.                     title="Go back"  
  33.                     onPress={() => this.props.navigation.goBack()}  
  34.                 />  
  35.             </View>  
  36.     );  
  37.     }  
  38. }  
  39.   
  40. const AppNavigator = createStackNavigator(  
  41.     {  
  42.         Home: HomeScreen,  
  43.         Profile: ProfileScreen  
  44.     },  
  45.     {  
  46.         initialRouteName: "Home"  
  47.     }  
  48. );  
  49.   
  50. const AppContainer = createAppContainer(AppNavigator);  
  51. export default class App extends React.Component {  
  52.     render() {  
  53.         return <AppContainer />;  
  54.     }  
  55. }  

Output:

React Native Moving Between Screens React Native Moving Between Screens

Comments

Popular Posts

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...

An introduction to Size Classes for Xcode 8

Introduction to Size Classes for Xcode In iOS 8, Apple introduced  size classes , a way to describe any device in any orientation. Size classes rely heavily on auto layout. Until iOS 8, you could escape auto layout. IN iOS8, Apple changed several UIKit classes to depend on size classes. Modal views, popovers, split views, and image assets directly use size classes to determine how to display an image. Identical code to present a popover on an iPad  causes a iPhone to present a modal view. Different Size Classes There are two sizes for size classes:  compact , and  regular . Sometime you’ll hear about any.  Any  is the generic size that works with anything. The default Xcode layout, is  width:any height:any . This layout is for all cases. The Horizontal and vertical dimensions are called  traits , and can be accessed in code from an instance of  UITraitCollection . The  compact  size descr...

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...