Home > Web Front-end > JS Tutorial > React Native AsyncStorage local storage tool class

React Native AsyncStorage local storage tool class

小云云
Release: 2018-05-17 16:54:01
Original
1980 people have browsed it

This article mainly shares the React Native AsyncStorage local storage tool class for everyone, which has certain reference value. Interested friends can refer to it and hope it can help everyone.

Use the react-native component AsyncStorage to save local data through promise. The specific content is as follows

import {AsyncStorage} from 'react-native';

export default class StorageUtil {

  /**
   * 保存一个Json对象
   * @param key
   * @param value
   * @param callback
   */
  static async saveJsonObject(key, value) {
    return await this.saveString(key, JSON.stringify(value));
  }


  /**
   * 获取一个Json对象
   * @param key
   * @param defaultObject
   */
  static async getJsonObject(key, defaultObject) {
    let result=null;
    try{
      result=await this.getString(key,null);
      result=await JSON.parse(result);
    }catch (err){
      if(defaultObject){
        return Promise.resolve(defaultObject);
      }else{
        return Promise.reject(err);
      }
    }
    return result;

  }


  /**
   * 保存一个值
   * @param key
   * @param value
   */
  static async saveString(key, value) {
    if (key != null && value != null) {
      //Key 与Value 都不为空
      try {
         await AsyncStorage.setItem(key, value)
      } catch (err) {
        return Promise.reject(err)
      }
      return Promise.resolve(true);
    } else {
      return Promise.reject({"msg": "Key and value can not be null"});
    }
  }

  /**
   * 获取一个值
   * @param key
   * @param defaultValue
   */
  static async getString(key, defaultValue) {
    let result = null;
    let noDataError = {"msg": "No value found !"};
    if (key != null) {
      result = await AsyncStorage.getItem(key);
      // console.log('get string result',result,defaultValue);
      return result ? result : defaultValue!=null ? defaultValue : Promise.reject(noDataError);
    } else {
      if (defaultValue) {
        return Promise.resolve(defaultValue);
      } else {
        return Promise.reject(noDataError);
      }
    }

  }


  /**
   * 移除一个值
   * @param key
   */
  static async remove(key) {
    let result = true;
    try {
      result = await AsyncStorage.removeItem(key);
    } catch (err) {
      return Promise.reject(err)
    }
    return result;
  }


  /**
   * 获取所有已存储
   */
  static async getAllKeys() {
    let result=true;
    try {
      result = await AsyncStorage.getAllKeys();
    } catch (err) {
      return Promise.reject(err)
    }
    return result;
  }

}
Copy after login

External calls

Save

StorageUtil.saveJsonObject(KEY_LOCAL_USER_INFO, user);
Copy after login

Read

StorageUtil.getJsonObject(KEY_LOCAL_USER_INFO).then(data=>{console.log(data))}
Copy after login

Clear

StorageUtil.remove(KEY_LOCAL_USER_INFO)
Copy after login

Related recommendations:

Example of LocalStorage local storage in html5

HTML5 implements the local storage function of the shopping cart

A brief discussion of the local storage of localStorage

The above is the detailed content of React Native AsyncStorage local storage tool class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template