简体   繁体   English

本地存储 - React Native

[英]Local Storage - React Native

As Async Storage is deprecated what other way is there to store a variable locally??由于不推荐使用异步存储,还有什么其他方式可以在本地存储变量?

I have a React Native ios App with a Notification Centre.我有一个带有通知中心的 React Native ios 应用程序。
Each time the user enters the Notification Centre a Cognito access Token was generated.每次用户进入通知中心时,都会生成一个 Cognito 访问令牌。 To avoid excess of token generation the Tokens were saved through Async storage and their expiry was checked.为避免生成过多的令牌,令牌通过异步存储保存并检查其到期时间。
Now is there some other local storage in React Native that i can use??现在我可以使用 React Native 中的其他本地存储吗?

Async storage is not deprecated its moved to a separate package which you can install and use异步存储未被弃用,它已移至可以安装和使用的单独 package

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

Or for tokens you can use react-native-keychain which is a way secure package you can check it here.或者对于令牌,您可以使用 react-native-keychain 这是一种安全的 package 方式,您可以在此处查看。 https://github.com/oblador/react-native-keychain https://github.com/oblador/react-native-keychain

It is moved to @react-native-community/async-storage它被移动到@react-native-community/async-storage

Install it and import it from lib:安装它并从 lib 中导入它:

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

Async storage from react-native library is deprecated, they split it from react-native core into a community library. react-native库中的异步存储已被弃用,他们将其从 react-native 核心拆分为社区库。

You can always use Async Storage from this library您始终可以使用此中的异步存储

Just follow installation steps from docs .只需按照docs中的安装步骤操作即可。 And you can import AsyncStorage like this你可以像这样导入AsyncStorage

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

AsyncStorage is deprecated so use below package AsyncStorage 已弃用,因此请在 package 以下使用

Get library With Yarn:使用 Yarn 获取库:

yarn add @react-native-async-storage/async-storage

Once done, import the header完成后,导入 header

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

For store a value用于存储值

const storeData = async (value) => {
  try {
    await AsyncStorage.setItem('@storage_Key', value)
  } catch (e) {
    // saving error
  }
}

Get a Value获取价值

const getData = async () => {
  try {
    const value = await AsyncStorage.getItem('@storage_Key')
    if(value !== null) {
      // value previously stored
    }
  } catch(e) {
    // error reading value
  }
}

For more: offical link更多: 官方链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM