通过文档: https ://facebook.github.io/react-native/docs/native-modules-android.html
在调用方法来访问并因此使用 NativeModule 时,找不到它......
'use strict';
import {NativeModules, DeviceEventEmitter} from 'react-native'
const MessengerModule = NativeModules.MessengerModule
const Messenger = {};
Messenger.show = (message, duration) => {
console.log(NativeModules.MessengerModule) //undefined
MessengerModule.show(message, duration);
};
module.exports = Messenger
undefined
记录...以及一个不错的小错误:
ReactNativeJS: undefined is not an object (evaluating 'MessengerModule.show')
显然,不完全是我想要的结果。
Java 模块:
package com.app.firebase;
import android.widget.Toast;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import java.util.Map;
public class MessengerModule extends ReactContextBaseJavaModule {
private static final String DURATION_SHORT_KEY = "SHORT";
private static final String DURATION_LONG_KEY = "LONG";
public MessengerModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "MessengerModule";
}
@ReactMethod
public void show(String message, int duration) {
Toast.makeText(getReactApplicationContext(), message, duration).show();
}
}
Java 包:
package com.app.firebase;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MessengerPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new MessengerModule(reactContext));
return modules;
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
有人可以帮我找到代码中的错误,或者可能缺少什么,从而使模块正常工作吗?一如既往,非常感谢任何方向和/或参考点!