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 command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
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
npx pod-install
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
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
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. 🙂
How do you mean without mounting gain? RNRestart reloads the React Native Bundle which restart the whole application so everything will mount again.
Hey Hi, Nick,
Are you sure? I haven’t found any perfect document for that and as per my experience, it is not mounting the app and starts the screen from initial.
Man. You saved me