【问题标题】:Android undefined is not an object evaluating custom module - React NativeAndroid undefined 不是评估自定义模块的对象 - React Native
【发布时间】: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 installreact-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


【解决方案1】:

我想通了!在Module.java 我返回了错误的课程。

曾经:

public String getName() {
    return "RNGifMaker";
}

应该是:

 public String getName() {
    return "RNGifMakerModule";
  }

【讨论】:

    猜你喜欢
    • 2016-10-14
    • 2022-01-23
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    相关资源
    最近更新 更多