如何解决使用 AsyncStorage (deprecated) 警告?使用社区(正确)库

Nat*_*lea 4 warnings react-native asyncstorage

我正在使用 @react-native-community/async-storage 并不断收到此警告:

Warning: Async Storage has been extracted from react-native core and will be removed in a future release. It can now be installed and imported from '@react-native-community/async-storage' instead of 'react-native'. See https://github.com/react-native-community/react-native-async-storage

这是我的 package.json:

{
  "name": "mobile_app",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "@aws-amplify/pushnotification": "^1.0.32",
    "@react-native-community/async-storage": "^1.6.1",
    "aws-amplify": "^1.1.32",
    "aws-amplify-react-native": "^2.1.15",
    "buffer": "^5.3.0",
    "moment": "^2.24.0",
    "react": "16.8.3",
    "react-native": "0.59.9",
    "react-native-ble-plx": "^1.0.3",
    "react-native-calendars": "^1.208.0",
    "react-native-check-box": "^2.1.7",
    "react-native-dialog": "^5.6.0",
    "react-native-gesture-handler": "^1.3.0",
    "react-native-indicators": "^0.13.0",
    "react-native-keyboard-aware-scroll-view": "^0.8.0",
    "react-native-modal-datetime-picker": "^7.5.0",
    "react-native-modalbox": "^1.7.1",
    "react-native-swipeable-row": "^0.8.1",
    "react-native-vector-icons": "^6.6.0",
    "react-navigation": "^3.11.0",
    "react-redux": "^7.1.0",
    "redux": "^4.0.1",
    "redux-logger": "^3.0.6",
    "redux-saga": "^1.0.5"
  },
  "devDependencies": {
    "@babel/core": "^7.4.5",
    "@babel/runtime": "^7.4.5",
    "babel-jest": "^24.8.0",
    "jest": "^24.8.0",
    "metro-react-native-babel-preset": "^0.54.1",
    "react-test-renderer": "16.8.3"
  },
  "jest": {
    "preset": "react-native"
  },
  "rnpm": {
    "assets": [
      "./assets/fonts/"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我删除了 node_modules 和 yarn.lock 并重新安装。此外,我查看了我所有的依赖项(正如在这个问题中所推荐的那样:如何删除“警告:异步存储已从 react-native 核心中提取...”?),并且它们都没有使用已弃用的异步存储包。

您对如何解决此警告有任何建议吗?

---- 编辑:有人问我如何导入 AsyncStorage。我只在名为 storage-helpers 的文件中的一个地方执行此操作:

import AsyncStorage from '@react-native-community/async-storage';

set_data = async (storage_key, value) => {
    try {
        const value_to_store = JSON.stringify(value);
        return await AsyncStorage.setItem(storage_key, value_to_store);
    } catch (error) {
        console.log(error);
        return error;
    }
}

get_data = async (storage_key) => {
    console.log("Getting Data", storage_key);
    const value = await AsyncStorage.getItem(storage_key)
        .then((returned_value) => {
            const parsed = JSON.parse(returned_value);
            return parsed;
        })
        .catch((error) => {
            console.log("Get Item Error: ", error);
        })
    console.log("Finished Getting Data");
    return value;
}

clear_data = async () => {
    console.log("Clearing Persistent Storage");
    return await AsyncStorage.clear();
}

module.exports = {
    set_data,
    get_data,
    clear_data,
}
Run Code Online (Sandbox Code Playgroud)

And*_*rew 9

更新

\n

现在有一个新的异步存储存储库,可以在此处找到

\n

https://github.com/react-native-async-storage/async-storage

\n

用法(注意依赖项的新名称)

\n
import AsyncStorage from \'@react-native-async-storage/async-storage\';\n
Run Code Online (Sandbox Code Playgroud)\n

查看有关安装、链接说明和使用的文档。

\n

https://react-native-async-storage.github.io/async-storage/docs/install/

\n

https://react-native-async-storage.github.io/async-storage/docs/usage

\n

\xe2\x80\x94\xe2\x80\x94

\n

查看您的依赖项列表,列表中的第一个@aws-amplify/pushnotification使用AsyncStoragefromreact-native

\n

您可以在这里看到它导入了AsyncStorage. 下面是确切的行:

\n
import { NativeModules, DeviceEventEmitter, AsyncStorage, PushNotificationIOS, Platform, AppState } from \'react-native\';\n
Run Code Online (Sandbox Code Playgroud)\n

这就是您收到警告的原因,这是由于使用了这种依赖性AsyncStorage

\n

五月份曾就此问题提出过一个问题,但此后已被标记为已关闭。您可以在此处查看该问题。

\n
\n

似乎@aws-amplify/core也使用AsyncStoragefrom react-native

\n

您可以在此处的RNComponents和此处的StorageHelper 中看到它的使用。

\n
\n

您最好的做法是分叉存储库并修复 AsyncStorage 的所有实例以使用正确的版本,或者提出问题。

\n
\n

仔细检查您的依赖项以了解它们实际使用的内容总是值得的。有时只需搜索他们已安装的内容或 github 上的内容就足够了。

\n


Mik*_*e M 5

确保您使用正确的导入:

import AsyncStorage from '@react-native-community/async-storage';
Run Code Online (Sandbox Code Playgroud)