AsyncStorage简析
来源:互联网 发布:对网络直播的利与弊 编辑:程序博客网 时间:2024/06/03 13:57
今天是8月了,又是很久没有更新日志了呢,这样真是太怠惰了!
所以今天,笔者就来讲一讲react native中的asyncStorage的大概用法吧~
记得官方的文档说,asyncStorage最好由使用者自行封装一层再进行使用,因为asyncStorage只支持键值对的存储(key — value),而且这个value只支持字符串的存储……不过这不是还有一个JSON的stringify嘛,其实这个根本算不了什么。
下面上代码吧,笔者已经将他封装成了一个js模块,用起来还是比较方便的,而且官方文档推荐全局使用一个asyncStorage,所以我们可以在项目的入口处直接使用:
Import DeviceStorage from './util/DeviceStorage';global.Storage = DeviceStorage;
然后接下来直接用 Storage.save/clear/等等的方法就行啦,并不需要再次import 了!
附上DeviceStorage的源码。
import {AsyncStorage} from 'react-native';export default class DeviceStorage { static get(key) { return AsyncStorage.getItem(key).then((value) => { const jsonValue = JSON.parse(value); return jsonValue; }); } static getString(key){ return AsyncStorage.getItem(key).then((value) => { return value; }); } static save(key, value) { return AsyncStorage.setItem(key, JSON.stringify(value)); } static saveString(key,value){ return AsyncStorage.setItem(key, value); } static update(key, value) { return DeviceStorage.get(key).then((item) => { value = typeof value === 'string' ? value : Object.assign({}, item, value); return AsyncStorage.setItem(key, JSON.stringify(value)); }); } static clear(key){ return AsyncStorage.removeItem(key).then(val=>{return val;}); }}
阅读全文
0 0
- AsyncStorage简析
- AsyncStorage尝试
- ReactNative(API)AsyncStorage存储
- react native AsyncStorage
- React-Native AsyncStorage使用
- React Native之AsyncStorage
- React-Native AsyncStorage setNativeProps
- React-Native AsyncStorage setNativeProps
- Promise and AsyncStorage React Native
- React-native 之 AsyncStorage学习
- React Native——AsyncStorage
- react native AsyncStorage 使用详解
- react-native-AsyncStorage<持久化数据存储>
- AsyncStorage存储key管理小技巧
- 《React-Native系列》28、 RN之AsyncStorage
- React Native使用AsyncStorage本地持久化
- 《React-Native系列》28、 RN之AsyncStorage
- React Native使用AsyncStorage本地持久化
- Java List遍历的三种方式
- Java web入门——Servlet
- ROS下实现机器人序列任务的执行控制
- 455. Assign Cookies
- HDOJ 2066 一个人的旅行 (Dijkstra)
- AsyncStorage简析
- jQuery节点创建与属性的处理
- python基础(一)数据类型和变量
- 计算网络面试总结1
- bzoj 3207(主席树+hash)
- Windows10下GPU版本TensorFlow安装问题汇总
- Linux tomcat安装详解
- 安卓使用约束者布局实现屏幕适配
- JDBC入门