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

标签 react-native warnings 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/"
    ]
  }
}

我删除了 node_modules 和 yarn.lock 并重新安装。此外,我查看了我的所有依赖项(正如在这个问题中所推荐的: How to remove 'Warning: Async Storage has been extracted from react-native core...'? )并且它们都没有使用已弃用的异步存储包。

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

- - 编辑:
有人问我如何导入 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,
}

最佳答案

确保您使用正确的导入:

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

关于react-native - 如何解决使用 AsyncStorage (deprecated) 警告?使用社区(正确)库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57578179/

相关文章:

php - 我可以让 PHP 的 PDO 返回 MySQL 警告,而不仅仅是错误消息吗?

javascript - 为什么函数在 AsyncStorage 完成之前运行

javascript - React Native FlatList 不渲染项目

delphi - 如何禁用 Delphi 中关于 "return value ... might be undefined"的警告?

android - 无法加载 exp ://Something went wrong

python - 如何从 setup.py 中抑制 PEP440Warning?

android - 使用 AsyncStorage 时, native react 保持每 10 毫秒重新渲染一次 View

javascript - 可能未处理的 promise 拒绝(id : 0) React Native AsyncStorage

ios - 无法将我的 React native 应用部署到应用商店 (iOS)

javascript - 想在 React Native CookieManager.clearAll() 中清除 webView 的缓存;不适用于 iOS