【发布时间】:2017-08-05 21:49:38
【问题描述】:
我正在尝试学习如何编写 react-native 模块,但我遇到了这个错误:
我的代码如下所示:
Module.java (../android/src/main/java/com/reactlibrary)
public class RNGifMakerModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
public RNGifMakerModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return "RNGifMaker";
}
@ReactMethod
public void alert(String message) {
Toast.makeText(getReactApplicationContext(), message, Toast.LENGTH_LONG).show();
}
}
index.android.js
import { RNGifMaker } from 'GifMaker';
const onButtonPress = () => {
RNGifMaker.alert('Hello World');
};
package.json
"dependencies": {
"react": "16.0.0-alpha.6",
"react-native": "0.43.3",
"GifMaker":"file:../"
},
index.js (../)
import { NativeModules } from 'react-native';
module.exports = NativeModules.RNGifMaker;
npm install 和 react-native link 我都运行过。
我(大部分)关注了this tutorial。
【问题讨论】:
-
RNGifMaker 应该源自文件 react-native.js 中的 NativeModules 还是源自名为 GifMaker.js 的文件?
-
它应该源自
../index.js -
那为什么要从 GifMaker.js 导入而不是从 index.androud.js 中的 index.js 导入呢?
-
GifMaker.js 不存在。我从
GifMaker导入它,我在package.json中设置了它。感谢您的帮助!
标签: android react-native npm