react native 原生模塊橋接的簡單說明

原文出自:https://github.com/prscX/awes...
博客鏈接:https://ssshooter.com/2019-02...

Android

創建原生模塊包

  • 通過繼承 ReactPackage 爲你的原生模塊包創建 Java 類,可以這麼寫:
  • 覆蓋 createNativeModulescreateViewManagers 方法

public class MyNativePackage implements ReactPackage {
    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
    }

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    }
}
  • createNativeModules 方法中添加原生模塊
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
    List<NativeModule> modules = new ArrayList<>();
    modules.add(new MyNativeModule(reactContext));

    return modules;
}
  • createViewManagers 方法中添加原生 UI 組件
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    List<ViewManager> components = new ArrayList<>();
    components.add(new RNNativeComponent());

    return components;
}

創建原生模塊

  • 先通過繼承 ReactContextBaseJavaModule 創建 MyNativeModule 類。
public class MyNativeModule extends ReactContextBaseJavaModule {
    public MyNativeModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }
}
  • 爲了讓 React Native 在 NativeModules 中找到我們的模塊,我們還需要覆蓋 getName 方法。
@Override
public String getName() {
    return "MyNativeModule";
}
  • 現在我們已經擁有一個可以導入到 JavaScript 代碼的原生模塊,現在可以向其中加入功能。
  • 暴露模塊方法:定義一個接受設置參數、成功回調和失敗回調的 Show 方法。
public class MyNativeModule extends ReactContextBaseJavaModule {
    @ReactMethod
    public void Show(ReadableMap config, Callback successCallback, Callback cancelCallback) {
        Activity currentActivity = getCurrentActivity();

        if (currentActivity == null) {
            cancelCallback.invoke("Activity doesn't exist");
            return;
        }
    }
}
  • 在 JavaScript 中調用模塊方法
import { NativeModules } from 'react-native'

const { MyNativeModule } = NativeModules

MyNativeModule.Show(
  {}, //Config Parameters
  () => {}, //Success Callback
  () => {} //Cancel Callback
)

注意:

  • 模塊方法只提供靜態引用,不包含於 react 樹中。

創建原生 UI 組件

  • 如果你需要在 react 樹中渲染一個原聲 UI 組件
  • 創建一個繼承 ReactNative ViewGroupManager 的 Java 類
public class RNNativeComponent extends ViewGroupManager<ViewGroup> {
    public static final String REACT_CLASS = "RNNativeComponent";
}
  • 覆蓋 getName 方法:
@Override
public String getName() {
    return REACT_CLASS;
}
  • 覆蓋 createViewInstance 方法,返回你的自定義原生組件
@Override
  protected FrameLayout createViewInstance(final ThemedReactContext reactContext) {
      return //用 FrameLayout 包裹的自定義原生組件
  }
  • 創建原生 prop 方法
  @ReactProp(name = "prop_name")
  public void setPropName(NativeComponent nativeComponent, propDataType prop) {
  }
  • JavaScript 中使用
import { requireNativeComponent } from "react-native"

const MyNativeComponent = requireNativeComponent("RNNativeComponent", RNNativeComponent, {
  nativeOnly: { }
})

<MyNativeComponent prop={this.props.prop}>

iOS

Macro

  • RCTBridgeModule: RCTBridgeModule 是一個 protocol,它爲註冊 bridge 模塊 RCTBridgeModule @protocol RCTBridgeModule 提供了一個接口
  • RCT_EXPORT_MODULE(js_name): 在 class implementation 時使用 bridge 註冊你的模塊。參數 js_name 是可選的,用作 JS 模塊的名稱,若不定義,將會默認使用 Objective-C 的 class 名
  • RCT_EXPORT_METHOD(method)RCT_REMAP_METHOD(, method): bridge 模塊亦可定義方法,這些方法可以作爲 NativeModules.ModuleName.methodName 輸出到 JavaScript。
RCT_EXPORT_METHOD(funcName:(NSString *)onlyString
                    secondName:(NSInteger)argInteger)
  { ... }

上面的例子暴露到 JavaScript 是 NativeModules.ModuleName.funcName

創建原生模塊包

我們需要在項目中添加兩個文件:頭文件和源文件。

- MyNativePackage.h

#import "RCTBridgeModule.h"

@interface MyNativePackage : NSObject <RCTBridgeModule>
@end

- MyNativePackage.m

#import "MyNativePackage.h"

@implementation MyNativePackage

RCT_EXPORT_MODULE();

@end

創建模塊方法

RCT_EXPORT_METHOD(Show:(RCTResponseSenderBlock)callback) {
}
  • JavaScript 中引入模塊方法
import { NativeModules } from 'react-native'

const MyNativeModule = NativeModules.MyNativeModule
MyNativeModule.Show(() => {})

創建原生 View 組件

  • 創建 view 方法,返回你的原聲組件
- (UIView *)view {
    //Initialize & return your native component view
}
  • 創建原生 prop 方法
RCT_CUSTOM_VIEW_PROPERTY(prop, DATA_TYPE_OF_PROP, YOUR_NATIVE_COMPONENT_CLASS) {
}
  • 在 JavaScript 中使用
import { requireNativeComponent } from "react-native"

const MyNativeComponent = requireNativeComponent("RNNativeComponent", RNNativeComponent, {
  nativeOnly: { }
})

<MyNativeComponent prop={this.props.prop}>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章