xaviervia / async-storage

An asynchronous, persistent, key-value storage system for React Native.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

React Native Async Storage

An asynchronous, unencrypted, persistent, key-value storage system for React Native.

Supported platforms

Getting Started

Install

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

Link

  • React Native 0.60+

CLI autolink feature links the module while building the app.

Use CocoaPods to add the native RNAsyncStorage to your project:

$ npx pod-install
  • React Native <= 0.59
$ react-native link @react-native-community/async-storage

Note: For macOS and Windows the manual linking is currently the only linking option.

See docs for manual linking guide.

Upgrading to React Native 0.60+

React Native 0.60+ comes with autolinking feature, which automatically links Native Modules in your project. In order to get it to work, make sure you unlink Async Storage first (if you had linked it before):

$ react-native unlink @react-native-community/async-storage

Usage

AsyncStorage can only store string data, so in order to store object data you need to serialize it first. For data that can be serialized to JSON you can use JSON.stringify() when saving the data and JSON.parse() when loading the data.

Importing

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

Storing data

setItem() is used both to add new data item (when no data for given key exists), and to modify exiting item (when previous data for given key exists).

Storing string value

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

Storing object value

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

Reading data

getItem returns a promise that either resolves to stored value when data is found for given key, or returns null otherwise.

Reading string value

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

Reading object value

getData = async () => {
  try {
    const jsonValue = await AsyncStorage.getItem('@storage_Key')
    return jsonValue != null ? JSON.parse(jsonValue) : null;
  } catch(e) {
    // error reading value
  }
}

Advanced usage

See docs for API and more examples or advanced usages.

Writing tests

Using Jest for testing? Make sure to check out docs on how to integrate it with this module.

Contribution

See the CONTRIBUTING file for how to help out.

License

MIT

About

An asynchronous, persistent, key-value storage system for React Native.

License:MIT License


Languages

Language:JavaScript 29.8%Language:Objective-C 28.4%Language:Java 17.1%Language:C++ 17.1%Language:Ruby 3.3%Language:Shell 2.2%Language:Starlark 1.2%Language:C 0.9%