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

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the libraries but need to install pods.

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

/*Example to Restart the screen in React Native*/
import React, {Component} from 'react';
import { Text, View, Button, StyleSheet } from 'react-native';
import RNRestart from 'react-native-restart';
  
export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = { counter: 0 };
  }
  componentDidMount() {
    this._interval = setInterval(() => {
      this.setState({
        //change the state of the laoding in every 1 second
        counter: this.state.counter + 1,
      });
    }, 1000);
  }

  componentWillUnmount() {
    clearInterval(this._interval);
  }

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

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.paragraph}>Example of React Native Restart</Text>
        <Text style={styles.paragraph}>{this.state.counter}</Text>
        <View>
          <Button title="Restart Screen" onPress={this.onButtonClick} />
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: 30,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    fontWeight: 'bold',
    textAlign: 'center',
  },
});

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
react-native run-ios (macOS only).

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. 🙂


Related Posts

2 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.