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

How I Reduced the Size of My React Native App by 85%

How and Why You Should Do It I borrowed 25$ from my friend to start a Play Store Developer account to put up my first app. I had already created the app, created the assets and published it in the store. Nobody wants to download a todo list app that costs 25mb of bandwidth and another 25 MB of storage space. So today I am going to share with you how I reduced the size of Tet from 25 MB to around 3.5 MB. Size Matters Like any beginner, I wrote my app using Expo, the awesome React Native platform that makes creating native apps a breeze. There is no native setup, you write javascript and Expo builds the binaries for you. I love everything about Expo except the size of the binaries. Each binary weighs around 25 MB regardless of your app. So the first thing I did was to migrate my existing Expo app to React Native. Migrating to React Native react-native init  a new project with the same name Copy the  source  files over from Expo project Install all de...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

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 = { ...