微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

React-Native:什么会导致模块为空?

如何解决React-Native:什么会导致模块为空?

我正在开发一个带有 expo 和 yarn 的 react native 项目,需要安装一个新的依赖项来获取设备制造商(如果你想知道它是 react-native-device-info)。我成功安装了它,并在我的代码中实现了它,但是在使用 expo go 打开应用程序时,我看到了一条错误消息

@React-native-community/react-native-device-info: NativeModule.RNDeviceInfo is null. To fix this issue follow these steps: *useless steps*

在 github 存储库中查找时,我发现了很多解决此问题的问题(并且回答它的开发人员不是他的问题,因为它适用于他的示例)。据我了解,错误是他对应的 Native module cannot be null。所以我的问题是:Native 模块怎么可能是空的?可能是什么原因?

我想自己解决这个问题,但不知道这个错误是如何产生的,也不知道本机模块是如何工作的,我什至不知道从哪里开始。我读到这可能是链接问题,但是 RN 0.59 以上的版本应该具有自动链接功能,甚至手动链接也无济于事。即使重新安装一切也无济于事。搜索他的代码,我找到了我将在这里留下的一段代码,这是触发错误的确切位置,但我不知道我们是如何到达那里的。

文件 nativeinterface.ts

    import { Platform,NativeModules } from 'react-native';
import { DeviceInfoNativeModule } from './privateTypes';

let RNDeviceInfo: DeviceInfoNativeModule | undefined = NativeModules.RNDeviceInfo;

// @ts-ignore
if (Platform.OS === 'web' || Platform.OS === 'dom') {
  RNDeviceInfo = require('../web');
}

if (!RNDeviceInfo) {
  // Produce an error if we don't have the native module
  if (
    Platform.OS === 'android' ||
    Platform.OS === 'ios' ||
    Platform.OS === 'web' ||
    // @ts-ignore
    Platform.OS === 'dom'
  ) {
    throw new Error(`@react-native-community/react-native-device-info: NativeModule.RNDeviceInfo is null. To fix this issue try these steps:
  • For react-native <= 0.59: Run \`react-native link react-native-device-info\` in the project root.
  • Rebuild and re-run the app.
  • If you are using CocoaPods on iOS,run \`pod install\` in the \`ios\` directory and then rebuild and re-run the app. You may also need to re-open Xcode to get the new pods.
  If none of these fix the issue,please open an issue on the Github repository: https://github.com/react-native-community/react-native-device-info`);
  }
}

export default RNDeviceInfo as DeviceInfoNativeModule;

如果您需要更多信息或者我有什么问题,请告诉我。

版本:

React Native => 0.62.2
expo => 38
react-native-device-info => 6.0.0
yarn => 1.22.5

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。