Restart/Reset Current Screen in React Native Without Mounting it Again

Restart/Reset Current Screen in React Native

Here is How to Restart/Reset Current Screen in React Native Without Mounting it Again. React Native does not provide the default refresh component to restart the screen. In some cases, we need to restart or reset the screen again and in that case, this example will help you to restart or reset the current screen.

If you are using React Navigation then you have a choice to mount the screen again and to refresh the screen have a look at Refresh Previous Screen after Going Back in React Navigation but in the normal case, if you need to refresh the screen then react-native-restart can help you by providing RNRestart Component.

To Import the Component

import RNRestart from 'react-native-restart';

To Restart or Reset the Current Screen

RNRestart.Restart();

In this example, we are going to make a single screen with a setInterval which will update the counter in every second. We will have a button below the counter and on the press of the button, we will refresh the screen.

Now let’s get started with the example.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use RNRestart we have to install react-native-restart dependency.

To install the dependencies open the terminal and jump into your project

cd ProjectName

Now install the dependency

npm install react-native-restart --save

CocoaPods Installation

In this example, we need to install the pods for react-native-restart. Please use the following command to install CocoaPods

cd ios && pod install && cd ..

Code to Restart/Reset Current Screen

Now Open App.js in any code editor and replace the code with the following code.

App.js

// Restart/Reset Current Screen in React Native Without Mounting it Again
// https://aboutreact.com/react-native-restart-reset-current-screen/

// import React in our code
import React, {useState, useEffect} from 'react';

// import all the components we are going to use
import {
  SafeAreaView,
  Text,
  View,
  Button,
  StyleSheet,
  TextInput,
} from 'react-native';

import RNRestart from 'react-native-restart';

const App = () => {
  const [value, setValue] = useState('');

  useEffect(() => {
    // Resetting default value for the input on restart
    setValue('Default Value');
  }, []);

  const onButtonClick = () => {
    RNRestart.Restart();
  };

  return (
    <SafeAreaView style={{flex: 1}}>
      <View style={styles.container}>
        <View style={styles.container}>
          <Text style={styles.heading}>
            Example of React Native Restart
          </Text>
          <Text style={styles.paragraph}>
            Insert any value in Input and Click Restart Screen. 
            You will see blank TextInput again.
          </Text>
          <TextInput
            placeholder="Please Insert Something"
            defaultValue={value}
            placeholderTextColor="#808080"
            underlineColorAndroid="transparent"
            onChangeText={(text) => setValue(text)}
            style={styles.textInputStyle}
          />
          <View style={{marginTop: 20}}>
            <Button
              title="Restart Screen"
              onPress={onButtonClick}
            />
          </View>
        </View>
        <Text
          style={{
            fontSize: 18,
            textAlign: 'center',
            color: 'grey'
          }}>
          Restart/Reset Current Screen in React Native
        </Text>
        <Text
          style={{
            fontSize: 16,
            textAlign: 'center',
            color: 'grey'
          }}>
          www.aboutreact.com
        </Text>
      </View>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    padding: 10,
  },
  textStyle: {
    textAlign: 'center',
    fontSize: 18,
    color: 'black',
  },
  heading: {
    fontSize: 18,
    fontWeight: 'bold',
    textAlign: 'center',
  },
  paragraph: {
    fontSize: 16,
    marginVertical: 16,
    textAlign: 'center',
  },
  textInputStyle: {
    fontSize: 25,
    textAlign: 'center',
    height: 50,
    backgroundColor: '#d6d6d6',
  },
});

export default App;

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

To run the project on an Android Virtual Device or on real debugging device

react-native run-android

or on the iOS Simulator by running (macOS only)

react-native run-ios

Download Source Code

Output Screenshots

Image   Image

This is How to Restart/Reset Current Screen in React Native Without Mounting it Again. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

3 thoughts on “Restart/Reset Current Screen in React Native Without Mounting it Again”

  1. How do you mean without mounting gain? RNRestart reloads the React Native Bundle which restart the whole application so everything will mount again.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.