模板版本:v0.4.0

react-native-safe-module

本项目基于 react-native-safe-module 开发。

该第三方库的仓库已迁移至 Gitcode,且支持直接从 npm 下载,新的包名为:@react-native-ohos/react-native-safe-module 版本所属关系如下:

三方库名称 三方库版本 发布信息 支持RN版本 Autolink 编译API版本 社区基线版本 npm地址
@react-native-ohos/react-native-safe-module ~ 1.3.0 Gitcode Releases 0.77.*/0.82.* API12+ 1.2.0 Npm Address
@react-native-ohos/react-native-safe-module ~ 1.2.1 Gitcode Releases 0.72.* API12+ 1.2.0 Npm Address
@react-native-oh-tpl/react-native-safe-module <= 1.2.0-0.0.1@deprecated Github Releases(deprecated) 0.72.* API12+ 1.2.0 Npm Address

1. 安装与使用

进入到工程目录并输入以下命令:

npm

npm install @react-native-ohos/react-native-safe-module

yarn

yarn add @react-native-ohos/react-native-safe-module

下面的代码展示了这个库的基本使用场景:

使用时 import 的库名不变。

import React from 'react';
import { View } from 'react-native';
import SafeModule from 'react-native-safe-module';

const App = () => {
  const NativeLottieView = SafeModule.component({
    viewName: 'LottieAnimationView',
    mockComponent: View,
  });
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <NativeLottieView
        style={{backgroundColor: 'red', width: 100, height: 100}}
      />
    </View>
  );
};

export default App;

2. 约束与限制

2.1. 兼容性

本文档内容基于以下环境验证通过:

  1. RNOH: 0.72.96; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112;
  2. RNOH: 0.72.33; SDK: HarmonyOS NEXT B1; IDE: DevEco Studio: 5.0.3.900; ROM: Next.0.0.71;
  3. RNOH: 0.77.18; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112;
  4. RNOH: 0.82.22; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.878; ROM: 6.0.0.130;

3. 属性

"Platform"列表示该属性在原三方库上支持的平台。

"HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。

Name Description Type Required Platform HarmonyOS Support
SafeModule.create(options) 创建Module Object Yes iOS/Android Yes

options:SafeModule.create(options) 方法的参数

Name Description Type Required Platform HarmonyOS Support
options.moduleName 需在 NativeModules 命名空间中查找的模块名称或名称数组。 string / Array<string> Yes iOS/Android Yes
options.mock 该原生模块的模拟实现 object Yes iOS/Android Yes
options.getVersion 用于获取原生模块版本的函数。仅在指定了覆盖配置且未在原生模块中导出 VERSION 属性时需要设置。默认值为 x => x.VERSION。 Function No iOS/Android Yes
options.overrides 版本号与对应属性/方法覆写实现的映射表。若被覆写的属性或方法为函数类型,该函数将在 SafeModule.create(...) 执行期间被调用,并接收两个参数:原始模块中该属性的初始值及原始模块本身。此函数的返回值将被设置为 SafeModule.create(...) 的返回结果。 object No iOS/Android Yes
options.isEventEmitter 标识原生模块是否为 EventEmitter 的标记。若启用,会将 EventEmitter 实例挂载至生成模块的 emitter 属性。默认值为 false。 boolean No iOS/Android Yes

4. 遗留问题

5. 其他

6. 开源协议

本项目基于 MIT License ,请自由地享受和参与开源。