Restart Your React Native Project
Using React Native version below 0.40? use version 0.0.1. Otherwise, use version 0.0.2.
npm install react-native-restart@0.0.1 --save
npm install react-native-restart --save
react-native link react-native-restart
or npm install -g rnpm && rnpm link react-native-restart
In android/settings.gradle
...
include ':react-native-restart'
project(':react-native-restart').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-restart/android')
In android/app/build.gradle
...
dependencies {
...
compile project(':react-native-restart')
}
Register module (in MainActivity.java
)
import com.avishayil.rnrestart.ReactNativeRestartPackage; // <--- Import
public class MainActivity extends ReactActivity {
......
/**
* A list of packages used by the app. If the app uses additional views
* or modules besides the default ones, add more packages here.
*/
@Override
protected List<ReactPackage> getPackages() {
...
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ReactNativeRestartPackage() // Add this line
);
}
......
}
Register module (in MainApplication.java
)
import com.avishayil.rnrestart.ReactNativeRestartPackage; // <--- Import
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
......
/**
* A list of packages used by the app. If the app uses additional views
* or modules besides the default ones, add more packages here.
*/
@Override
protected List<ReactPackage> getPackages() {
...
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ReactNativeRestartPackage() // Add this line
);
}
};
......
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
};
- Drag the file
RCTRestart.xcodeproj
from/node_modules/react-native-restart/ios
into theLibraries
group in the Project navigator. Ensure thatCopy items if needed
is UNCHECKED!
- Ensure that
libRCTRestart.a
is linked throughLink Binary With Libraries
onBuild Phases
:
-
Ensure that
Header Search Paths
onBuild Settings
has the path$(SRCROOT)/../node_modules/react-native-restart
set torecursive
: -
You're All Set!
import RNRestart from 'react-native-restart'; // Import package from node modules
// Immediately reload the React Native Bundle
RNRestart.Restart();
Thanks to Microsoft CodePush library. I simply extracted the code from their library's logic to reload the React Native Bundle.
- Tell me?