【发布时间】:2021-10-06 02:58:36
【问题描述】:
我正在开发一个带有 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。所以我的问题是:原生模块怎么可能是空的?可能是什么原因?
我想自己解决这个问题,但是不知道这个错误是如何产生的,或者原生模块是如何工作的,我什至不知道从哪里开始。我读到这可能是一个链接问题,但是 0.59 以上的 RN 版本应该有自动链接,甚至手动链接也没有帮助。即使重新安装所有内容也无济于事。搜索他的代码,我找到了我将在这里留下的一段代码,那是触发错误的确切位置,但我不知道我们是如何到达那里的。
文件: 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
【问题讨论】:
标签: typescript react-native expo node-modules yarnpkg