\`unable to resolve module @react-native-community/async-storage\` 破坏了我的 React Native 环境

新手上路,请多包涵

在此处输入图像描述

我基本上和这里有同样的问题。

https://github.com/firebase/firebase-js-sdk/issues/1899

我是如何得到这个错误的

由于 AsyncStorage 已弃用,我尝试按照 官方文档 安装 @react-native-community/async-storage

但它完全失败了,因为我得到了上面的错误。因此,我想回滚到我以前的工作版本,除了我所做的没有任何工作。

这些都没有解决我的问题

  1. 错误屏幕上建议的 4 个命令
  2. 撤消 yarn addyarn remove
  3. 我也做了 npm install @react-native-community/async-storage ,没用。 3.5 所以我做了 npm uninstall @react-native-community/async-storage 它被删除了,但回滚不起作用。
  4. 重新安装 react-native-cli
  5. 从头开始重新创建一个全新的项目,但它仍然给出相同的错误。

我找不到解决方案。请帮忙。

原文由 Leonard 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
1 个回答

如果它在 iOS 你可能忘了做 pod install

将此粘贴到 ios/Podfile

   pod 'RNCAsyncStorage', :path => '../node_modules/@react-native-community/async-storage'

然后就做 cd ios && pod install

编辑。

我从头开始创建了一个项目,这是使 asyncStorage 在 iOS 和 Android 上运行的步骤:

  1. react-native init AsyncTest

  2. npm i @react-native-community/async-storage

(在此步骤中尝试使用 asyncStorage 会显示错误,但适用于 Android)

  1. 将这个 pod 粘贴到 Podfile 中:
   pod 'RNCAsyncStorage', :path => '../node_modules/@react-native-community/async-storage'

  1. 从终端,假设您在项目文件夹中执行 cd iospod install

5)项目在iOS上成功运行并且可以工作。

react-native 版本是 0.60.4

这是项目测试 App.js 用于测试的方式:

 import React from 'react';
import { View } from 'react-native';
import AsyncStorageTest from './AsyncStorageTest'
const App = () => {
  return (
    <View>
      <AsyncStorageTest />
    </View>
  );
};

export default App

AsyncStorageTest 是:

 import React, { Component } from 'react'
import { View, Text, Button } from 'react-native'
import AsyncStorage from '@react-native-community/async-storage';

export class AsyncStorageTest extends Component {
    constructor(props) {
        super(props)
        this.state = {
            storedData: "myValue"
        }
    }

storeData = async () => {
    console.log("inside storeData")
    try {
        await AsyncStorage.setItem('Test', 'TestValue')
    } catch (e) {
        console.log(e)
    }
}

getData = async () => {
    console.log("inside getData")
    try {
        const value = await AsyncStorage.getItem('Test')
        this.setState({ storedData: value })

    } catch (e) {
        // error reading value
    }
}

render() {
    return (
        <View style={{ marginTop: 40 }}>
            <Text> {this.state.storedData}</Text>
            <Button title={"storeData"} onPress={this.storeData}></Button>
            <Button title={"getData"} onPress={this.getData}></Button>
        </View>
    )
}
}

export default AsyncStorageTest

测试和工作,看看你是否错过了什么。

确保 @react-native-community/async-storage 与您的项目取消链接。

原文由 Auticcat 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题