基于 react-native-restart 的 OpenHarmony 适配版,运行时重启/重载应用
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 9 个月前 | ||
| 9 个月前 | ||
| 9 个月前 | ||
| 6 年前 | ||
| 6 年前 | ||
| 6 年前 | ||
| 3 年前 | ||
| 6 年前 | ||
| 9 个月前 | ||
| 3 年前 | ||
| 3 年前 | ||
| 6 年前 | ||
| 6 年前 | ||
| 4 个月前 | ||
| 9 个月前 | ||
| 3 年前 | ||
| 3 年前 | ||
| 9 个月前 | ||
| 9 个月前 | ||
| 3 年前 |
以下内容由 AI 翻译,如有问题请 点此提交 issue 反馈
React Native Restart
有时你需要在应用运行时重新加载应用包。这个包能帮你实现这一功能。
| iOS 动图 | Android 动图 |
|---|---|
![]() |
![]() |
安装
- 使用
react-native < 0.62?请安装react-native-restart@0.0.17 - 使用
0.71 > react-native >= 0.62?请安装react-native-restart@0.0.24 - 使用
react-native >= 0.71?请安装react-native-restart@0.0.27及以上版本
使用 yarn
$ yarn add react-native-restart
使用 npm
$ npm install --save react-native-restart
自动链接设置(react-native >= 0.60)
iOS
$ cd ios
$ pod install
Android
无需执行其他步骤
自动安装(无自动链接)
react-native link react-native-restart 或 npm install -g rnpm && rnpm link react-native-restart
手动 Android 安装
在 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; // <--- 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 RestartPackage() // Add this line
);
}
};
......
};
iOS 手动安装步骤
导入库文件
- 从
/node_modules/react-native-restart/ios路径下将Restart.xcodeproj文件拖拽到项目导航器中的Libraries分组下。确保 "Copy items if needed" 选项处于 未勾选 状态!


- 确保在 "Build Phases"(构建阶段)的 "Link Binary With Libraries"(链接二进制文件与库)中已添加
libRestart.a:

-
确保在 "Build Settings"(构建设置)的 "Header Search Paths"(头文件搜索路径)中,已将路径
$(SRCROOT)/../node_modules/react-native-restart设置为 "recursive"(递归): -
完成上述步骤即可!
iOS CocoaPod 安装步骤
在您的 ios/Podfile 文件中,请确保使用本地 node_modules/ 目录下的 react-native-restart。这样配置后,只需链接您项目的 Pod,无需其他额外配置:
target 'MyReactApp' do
# Make sure you're also using React-Native from ../node_modules
pod 'React', :path => '../node_modules/react-native', :subspecs => [
'Core',
'RCTActionSheet',
# ... whatever else you use
]
# React-Native dependencies such as yoga:
pod 'yoga', path: '../node_modules/react-native/ReactCommon/yoga'
# The following line uses react-native-restart, linking with
# the library and setting the Header Search Paths for you
pod 'react-native-restart', :path => '../node_modules/react-native-restart'
end
请记得运行 cd ios && pod install 以更新 Xcode 使用的文件。
用法
import RNRestart from 'react-native-restart'; // Import package from node modules
// Immediately reload the React Native Bundle
RNRestart.Restart(); // Deprecated
RNRestart.restart();
贡献指南
欢迎参与贡献。如果您希望为这个库贡献代码,请参阅 CONTRIBUTING.md。
致谢
感谢 Microsoft CodePush 库。我只是从他们库中提取了重新加载 React Native Bundle 的相关代码逻辑。

