react-native-restart

repository·master·Indexed 21 days ago

https://github.com/avishayil/react-native-restart

A utility library for React Native that allows developers to reload the app bundle during runtime to apply updates or refresh the application state. It provides the RNRestart module with methods including restart(), the deprecated Restart(), and getReason().

Tokens
1.8K
Snippets
10
Records
12
Agent score
76%

What's inside react-native-restart

  1. CocoaPod iOS Installation

    master

    To use CocoaPods for iOS, add the library to your ios/Podfile pointing to the local node_modules path. This handles linking and Header Search Paths automatically.

    After updating the Podfile, run cd ios && pod install.

    target 'MyReactApp' do
      # ... other pods
      pod 'react-native-restart', :path => '../node_modules/react-native-restart'
    end
  2. Manual Android Installation

    master

    If auto-linking is not available, follow these manual steps:

    1. Update android/settings.gradle to include the project.
    2. Update android/app/build.gradle to add the dependency.
    3. Register the module in MainApplication.java by adding RestartPackage to the getPackages() list.
    // android/settings.gradle
    include ':react-native-restart'
    project(':react-native-restart').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-restart/android')
    // android/app/build.gradle
    dependencies {
        implementation project(':react-native-restart')
    }
    // MainApplication.java
    import com.reactnativerestart.RestartPackage;
    
    // ... inside getPackages()
    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new RestartPackage() // Add this line
        );
    }
  3. Manual iOS Installation

    master

    If not using CocoaPods, follow these steps:

    1. Drag Restart.xcodeproj from node_modules/react-native-restart/ios into the Libraries group in your Xcode Project navigator. Uncheck Copy items if needed.
    2. In Xcode, go to Build Phases > Link Binary With Libraries and ensure libRestart.a is linked.
    3. In Build Settings, set Header Search Paths to $(SRCROOT)/../node_modules/react-native-restart and set it to recursive.
  4. Forcefully reload the app

    master

    While Fast Refresh handles automatic updates, you can perform a full reload to reset the app state:

    • Android: Press the <kbd>R</kbd> key twice or select "Reload" from the Dev Menu (accessed via <kbd>Ctrl</kbd> + <kbd>M</kbd> on Windows/Linux or <kbd>Cmd ⌘</kbd> + <kbd>M</kbd> on macOS).
    • iOS: Press <kbd>R</kbd> in the iOS Simulator.
  5. Install react-native-restart

    master

    Install the package using your preferred package manager. Choose the version compatible with your React Native version:

    • React Native < 0.62: react-native-restart@0.0.17
    • 0.62 <= React Native < 0.71: react-native-restart@0.0.24
    • 0.72 <= React Native <= 0.84: react-native-restart@0.0.28
    • React Native >= 0.85: react-native-restart@0.0.28 or above
    # Using yarn
    yarn add react-native-restart
    
    # Using npm
    npm install --save react-native-restart
  6. Configure Metro for monorepo support in the Example project

    master

    The metro.config.js in the Example directory is configured to support a monorepo structure. It uses watchFolders to include the parent directory in the Metro watcher and configures resolver.nodeModulesPaths to ensure that dependencies can be resolved from both the local Example/node_modules and the root node_modules directory.

    const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
    const path = require('path');
    
    const config = {
      watchFolders: [
        path.resolve(__dirname, '..'),
      ],
      resolver: {
        nodeModulesPaths: [
          path.resolve(__dirname, 'node_modules'),
          path.resolve(__dirname, '..', 'node_modules'),
        ],
      },
    };
    
    module.exports = mergeConfig(getDefaultConfig(__dirname), config);
  7. Restart the React Native application with RNRestart

    master

    The RNRestart module provides methods to reload the React Native application bundle.

    • restart(reason?: string): Restarts the application. You can optionally provide a reason string to indicate why the restart is occurring. This is the preferred method.
    • Restart(reason?: string): An alias for restart. This method is marked as deprecated; use restart instead.
    • getReason(): Returns a Promise<string> that resolves to the reason for the last restart.
    import RNRestart from 'react-native-restart';
    
    // Restart without a specific reason
    RNRestart.restart();
    
    // Restart with a specific reason
    RNRestart.restart('Updating configuration');
    
    // Get the reason for the last restart
    RNRestart.getReason().then((reason) => {
      console.log('Last restart reason:', reason);
    });