ReactNative(API)AsyncStorage存储
来源:互联网 发布:自制地图软件 编辑:程序博客网 时间:2024/04/29 21:52
AsyncStorage存储类似Android中的sharedpreference存储或者IOS中的NSDefaultUser不过ReactNative中的AsyncStorage只能存储字符串类型
常用方法:
getItem(key:string,callback?:?(error:?Error,result:?string)=>void) 静态方法,该通过key字段来进行查询存储的数据,把该结果值作为参数传入第二个callback方法。如果发生错误,会把Error对象传入callback方法。该方法最终返回一个Promise对象
setItem(key:string,value:string,callback?:?(error:?Error)=>void) 静态方法,该根据key字段设置value内容,完成之后进行回调callback方法。如果发生错误会把Error对象传入callback方法中。该方法返回一个Promise对象。
removeItem(key:string,callback?:?(error:?Error)=>void) 静态方法,根据key进行删除值,成功之后进行回调callback方法。如果发生错误会把Error对象传入callback方法中。该方法返回一个Promise对象。
/** * Created by Administrator on 2016/9/12. */import React, {Component} from 'react';import { StyleSheet, View, Text, AsyncStorage} from 'react-native';var keyName = 'name';var keyValue = '张三';class AsyncStorageG extends Component { constructor(props) { super(props); this.state = { result: '初始值' }; } render() { return ( <View style={{flex: 1}}> <Text style={styles.text} onPress={this.save.bind(this)}>存</Text> <Text style={styles.text} onPress={()=>this.load()}>取</Text> <Text style={styles.text} onPress={()=> this.remove()}>删除</Text> <Text style={styles.text} >存储的内容是------------------{this.state.result}</Text> </View> ) } /** * 存数据 */ save() { //this 是指当前对象 this2 = this; AsyncStorage.setItem(keyName, keyValue, function (error) { if (error) { alert('存储失败'); } else { this2.setState( { result: '数据已经保存,取出来看看吧!!!' } ) } }) } /** * 取数据 */ load() { //this 是指当前对象 this2 = this; AsyncStorage.getItem(keyName, function (error, result) { if (!error) { this2.setState( { result: result === null ? '数据已经删除,现在取的是空值' : result } ) } }) } /** * 两个参数: * 一个是要删除指定key内容 * 第二个是一个回调函数 */ remove() { //this 是指当前对象 this2 = this; AsyncStorage.removeItem(keyName, function (error) { if (!error) { this2.setState( { result: '数据已经删除' } ) } }) }}const styles = StyleSheet.create({ text: { fontSize: 20 }});//es6写法export default AsyncStorageG;
0 0
- ReactNative(API)AsyncStorage存储
- ReactNative (API)AsyncStorage存储详解及实例
- ReactNative——数据存储AsyncStorage
- ReactNative学习实例(九)数据永久化存储AsyncStorage
- reactnative之AsyncStorage,简单使用以及坑
- react-native-AsyncStorage<持久化数据存储>
- AsyncStorage存储key管理小技巧
- React Native之轻量级存储AsyncStorage
- React Native之AsyncStorage本地存储
- React Native AsyncStorage本地存储工具类
- ReactNative(API)Alert
- ReactNative(API)Alert
- reactnative Navigator api解释
- ReactNative朋友圈API设计
- react native AsyncStorage 使用实例 异步存储数据以及读取
- React-Native AsyncStorage存储key管理小技巧
- RN 的持久化存储(AsyncStorage)的使用
- RN演示类似购物车清空的功能, 异步存储AsyncStorage
- c++标准库——容器类
- 排序算法时间复杂度总结
- IT新手入职的一些注意事项
- Cucumber java + Webdriver (7) 结合Page Object,使用LoadableComponent
- Android framework 修改系统声音最大值默认值以及系统亮度默认值最大值位置
- ReactNative(API)AsyncStorage存储
- [leetcode] 238. Product of Array Except Self
- Linux mint 18 安装NVIDIA显卡驱动
- 如何采集微信公众号文章
- mysql中procedure与function的区别
- TexturePacker 免费获取一年的密钥 TP:BF710A24
- HBase
- 简单描述JSON跟JSONP的区别以及实战
- iOS更新学习(3) 线上崩溃日处理