React Native + Android 原生模块:HMRClient 不是注册的可调用模块 [英] React Native + Android native module: HMRClient is not a registered callable module

查看:81
本文介绍了React Native + Android 原生模块:HMRClient 不是注册的可调用模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 ReactNative 开发本机模块.现在我正在玩本机代码部分.我已经使用 create-react-native-module 脚本创建了模块.我系统地收到以下错误:

I'm developing a native module for ReactNative. Right now I'm toying with the native code part. I have created the module with create-react-native-module script. I'm systematically getting the following error:

Invariant Violation: Module HMRClient is not a registered callable module (calling setup)

我已将问题归结为在 index.js 中为我的模块引用 NativeModules 时:

I have pinned down the problem to when I reference NativeModules inside the index.js for my module:

import { NativeModules } from 'react-native';
//const <...my module name...> = NativeModules.<...my module name...>;  // <---- NOT WORKING

class <...some js class...> {
    constructor() {
        const <...my module name...> = NativeModules.<...my module name...>;  // <-- WORKING
        <...my module name...>.debugMessage("Salve mondo advertising");
    }
}

也许是库加载时出现了一些奇怪的竞争条件?

Maybe some weird race condition on library loading?

针对此 HMRClient 错误的常见解决方法(在应用上禁用快速刷新、清理 gradlie 构建和/或 node_modules 等)似乎不起作用.

The common workarounds for this HMRClient error (disabling Fast Refresh on the app, cleaning the gradlie build and/or the node_modules, etc.) do not seem to work.

这是我的 react-native info 输出:

System:
    OS: Linux 4.15 Ubuntu 18.04.4 LTS (Bionic Beaver)
    CPU: (8) x64 Intel(R) Core(TM) i7-4810MQ CPU @ 2.80GHz
    Memory: 947.73 MB / 15.60 GB
    Shell: 4.4.20 - /bin/bash
  Binaries:
    Node: 11.10.1 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.8.0 - /usr/local/bin/npm
  SDKs:
    Android SDK:
      API Levels: 25, 28
      Build Tools: 25.0.2, 28.0.3, 29.0.0, 29.0.3
      System Images: android-19 | Intel x86 Atom, android-28 | Intel x86 Atom, android-28 | Intel x86 Atom_64, android-28 | Google Play Intel x86 Atom
  npmPackages:
    react: ^16.9.0 => 16.13.1 
    react-native: ^0.61.5 => 0.61.5 
  npmGlobalPackages:
    react-native-cli: 2.0.1

推荐答案

我遇到了类似的问题.

据我所知,你的模块没有在 npm 上发布,也没有从 node_modules 导入.

As I understand, your module didn't published on npm and didn't imported from node_modules.

当您将模块的 index.js 文件放置在它使用的 react-native 项目之外时,可能会发生这种情况.它导致 react-native 的双重导入.所以 registerCallableModule('HMRClient') 调用了两次.所以它导致这个错误

It may happen when you place index.js file of your module outside of react-native project, in which it used. It causes double import of react-native. So registerCallableModule('HMRClient') calls twice. So it causes this error

尝试将原生模块的 index.js 文件替换为 react-native 项目.对我有用

Try to replace index.js file of your native module to your react-native project. It works for me

这篇关于React Native + Android 原生模块:HMRClient 不是注册的可调用模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆