A simple module to restart your React Native app programmatically. This module works on both iOS and Android platforms, providing a unified API to restart the application when needed, such as after a configuration change or an error recovery action.
react-native-restart-app
allows you to restart your app from the code, which is especially useful when applying significant changes such as language updates, theme switching, or reloading configurations without requiring the user to manually restart the application.
For React Native version 0.60 and above, auto-linking will handle the linking of the library. Simply run the following command to install the module:
npm install react-native-restart-app
# or
yarn add react-native-restart-app
Run pod install
in the ios
directory to install the necessary pods:
cd ios/
pod install
For React Native versions 0.67 and below, you need to add an extra step in the Podfile
:
Add the following to your Podfile
:
pod 'react-native-restart-app', :path => '../node_modules/react-native-restart-app'
Then, run pod install
in the ios
directory to install the necessary pods:
cd ios/
pod install
Build your project using Xcode or via CLI:
npx react-native run-ios
If auto-linking is not supported or you are using an older version of React Native (<0.60), you can manually link the library.
- Open your project in Xcode and find the
Libraries
folder. - Right-click the folder and select "Add Files to [Your Project]".
- Navigate to
node_modules/react-native-restart-app/ios/
and addRNRestart.xcodeproj
. - In the "Build Phases" section of your project settings, add
libRNRestart.a
to "Link Binary With Libraries". - Perform a clean build with Xcode (
Cmd + Shift + K
) and then build the app again (Cmd + B
).
For Android, manual linking can be performed as follows:
-
Open
android/settings.gradle
and add:include ':react-native-restart-app' project(':react-native-restart-app').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-restart-app/android')
-
In
android/app/build.gradle
, add the following line inside thedependencies
block:implementation project(':react-native-restart-app')
-
Open
MainApplication.java
and import the package:import com.reactnativerestartapp.RNRestartPackage;
-
Then, add the package to your
getPackages
method:@Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new RNRestartPackage() // Add this line ); }
To use react-native-restart-app
, import it into your component and call the restart
method when needed.
import restart from 'react-native-restart-app';
// Example: Restart the app after a configuration change
const handleRestart = () => {
// Perform any necessary cleanup or configuration here
restart();
};
You can use the restart functionality after a language change in your app:
const changeLanguage = (language) => {
// Change app language or configuration
i18n.changeLanguage(language);
// Restart the app to apply the changes
restart();
};
This will cause the app to reload and apply any new configurations, such as a language change, theme change, or other critical updates.
This project is licensed under the MIT License.