React Native之持久化存储(AsyncStorage、react-native-storage)的使用
AsyncStorage是一个简单的、异步的、持久化的Key-Value存储系统,它对于App来说是全局性的。这是官网上对它的介绍。可以知道,这个asyncstorage也是以键值对的形式进行存储数据的。
1、封装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 save(key, value) {
return AsyncStorage.setItem(key, JSON.stringify(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 delete(key) {
return AsyncStorage.removeItem(key);
}
}
在其他组件中引用DeviceStorage类。
import DeviceStorage from './DeviceStorage';
2、保存
DeviceStorage.save("tel","18911112222");
3、获取
DeviceStorage.get('tel').then((tel)=>{
if(tel == null || tel == ''){
} else {
this.setState({
tel:tel,
});
}
})
4、更新
DeviceStorage.update("tel","17622223333");
5、删除
DeviceStorage.delete("tel");
案例二:
本案例中使用react-native-easy-toast进行简易弹窗提示。首先在项目中进行加载组件并引用;
1.在终端运行 npm i react-native-easy-toast --save
2.在要使用Toast的js文件中添加import Toast, {DURATION} from 'react-native-easy-toast'
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
View,
TextInput,
AsyncStorage,
Text
} from 'react-native';
import Toast,{DURATION} from 'react-native-easy-toast'; //引入Toast控件
//AsyncStorage是以键值对的形式保存数据 ,诸如安卓中SharedPreferences一样
const AS_KEY = "as_key";
export default class AsyncStoreDemo extends Component {
constructor(props) {
super(props);
}
//保存数据
asSave() {
AsyncStorage.setItem(AS_KEY, this.text, (error) => {
if (!error) {
this.toast.show('保存数据成功', DURATION.LENGTH_SHORT);
} else {
this.toast.show('保存数据失败', DURATION.LENGTH_SHORT);
}
});
}
//查询保存的数据
asQuery() {
AsyncStorage.getItem(AS_KEY, (error, result) => {
if (!error) {
if (result !== '' && result !== null) {
this.toast.show('查询到的内容是:' + result, DURATION.LENGTH_SHORT);
} else {
this.toast.show('未找到指定保存的内容!', DURATION.LENGTH_SHORT);
}
} else {
this.toast.show('查询数据失败', DURATION.LENGTH_SHORT);
}
});
}
//删除已经保存的数据
asDelete() {
AsyncStorage.removeItem(AS_KEY, (error) => {
if (!error) {
this.toast.show('删除数据成功', DURATION.LENGTH_SHORT);
} else {
this.toast.show('删除数据失败', DURATION.LENGTH_SHORT);
}
});
}
render() {
return (<View style={styles.container}>
<TextInput style={styles.edit}
//文字内容发生改变调用方法
onChangeText={text=>this.text=text}/>
<View style={styles.child}>
<Text style={styles.text}
onPress={()=>{
this.asSave()
}}>保存</Text>
<Text style={styles.text}
onPress={()=>{
this.asQuery()
}}>查询</Text>
<Text style={styles.text} onPress={()=>{
this.asDelete()
}}>删除</Text>
</View>
<Toast ref={toast=>{
this.toast=toast
}}/>
</View>);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop:60,
backgroundColor:'#e8e8e8'
},
child: {
flexDirection: 'row'
},
edit: {
fontSize: 20,
borderWidth: 1,
borderColor: '#d1d1d1',
margin: 10,
paddingLeft: 5,
height: 45,
borderRadius:3
},
text: {
fontSize: 20,
color: '#333',
marginLeft: 15
}
});
案例三:react-native-storage
React Native中Storage使用详解和封装
在移动端开发中,数据库存储肯定是避免不了的需求,在iOS中,我们也经常使用NSUserDefault单利类来存储一些简单的用户信息等数据,在web开发中我们经常使用LocalStorage来存储简单数据,在React Native中,我们可以选择直接使用官方推荐的数据存储组件AsyncStorage组件,但是有时候使用起来还是不够简单,功能不够多,这时我们就会选择封装一个storage,我们选择使用三方的react-native-storage来进一步封装
react-native-storage 官方文档
https://github.com/sunnylqm/react-native-storage/blob/master/README-CHN.md
网上大佬提供封装好的storage组件Demo示例
https://github.com/guangqiang-liu/react-native-storage-Demo
安装react-native-storage组件
npm install react-native-storage --saveimport Storage from 'react-native-storage'import { AsyncStorage } from 'react-native'import {sync} from './sync'
React Native之持久化存储(AsyncStorage、react-native-storage)的使用的更多相关文章
- React Native是一套使用 React 构建 Native app 的编程框架
React Native是一套使用 React 构建 Native app 的编程框架 React Native at first sight what is React Native? 跟据官方的描 ...
- React学习笔记-1-什么是react,react环境搭建以及第一个react实例
什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...
- 如何使用TDD和React Testing Library构建健壮的React应用程序
如何使用TDD和React Testing Library构建健壮的React应用程序 当我开始学习React时,我努力的一件事就是以一种既有用又直观的方式来测试我的web应用程序. 每次我想测试它时 ...
- Web持久化存储Web SQL、Local Storage、Cookies(常用)
在浏览器客户端记录一些信息,有三种常用的Web数据持久化存储的方式,分别是Web SQL.Local Storage.Cookies. Web SQL 作为html5本地数据库,可通过一套API来操纵 ...
- weblogic.nodemanager.common.ConfigException: Native version is enabled but nodemanager native library could not be loaded 解决办法
近日在一个原本工作正常的weblogic web server(操作系统为redhat 64位系统)上折腾安装redis/hadoop等东东,yum install了一堆第3方类库后,重启weblog ...
- IOS开发-本地持久化存储sqlite应用
前言 需求描述 开发测试环境 FMDB介绍 创建工程 一.前言 上一章介绍了如何开发一个IOS应用的入门案例教程: 我的第一个IOS开发应用 本章主要将介 ...
- iOS数据持久化存储:归档
在平时的iOS开发中,我们经常用到的数据持久化存储方式大概主要有:NSUserDefaults(plist),文件,数据库,归档..前三种比较经常用到,第四种归档我个人感觉用的还是比较少的,恰恰因为用 ...
- ios开发之数据的持久化存储机制
IOS中数据的持久化保存这块内容,类似于Android中文件的几种常见的存储方式. 对于数据的持久化存储,ios中一般提供了4种不同的机制. 1.属性列表 2.对象归档 3.数据库存储(SQLite3 ...
- iOS开发——数据持久化Swift篇&使用Core Data进行数据持久化存储
使用Core Data进行数据持久化存储 一,Core Data介绍 1,Core Data是iOS5之后才出现的一个数据持久化存储框架,它提供了对象-关系映射(ORM)的功能,即能够将对象转化成 ...
随机推荐
- PHP urlencode
url get传参时,对参数值需要用urlencode()处理,防止参数中含有特殊字符&等 例如: 一产品名称为A&T Plastic,在产品列表中就产生了这样的一个联接<a h ...
- 把xml格式的字符串写入到一个xml文件中
package demo; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; impo ...
- 第十篇:顺序容器vector,deque,list的选用规则
前言 常见的顺序容器主要有三种 - vector,deque,list.它们实现的功能相差不大,那么实际开发中该如何进行选择呢?本文将为你解答这个问题. 分析 由于这三种容器实现的数据结构原型不同(v ...
- 免费iOS第三方推送工具Urban Airship使用教程
本文转载至 http://blog.csdn.net/mamong/article/details/8542404 http://www.dapps.net/dev/iphone/ios-free ...
- Android测试:从零开始1——简介
参考文档:https://developer.android.com/training/testing/start/index.html 测试分类 使用android studio进行测试,首先需要先 ...
- 【BZOJ4540】[Hnoi2016]序列 莫队算法+单调栈
[BZOJ4540][Hnoi2016]序列 Description 给定长度为n的序列:a1,a2,…,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是指序列:al,al+1,…,a ...
- fineReport---sql
一.开窗函数-逐层平均 在创建数据集时用sql的开窗排名函数[AVG(字段) over(PARTITION BY 分组字段 order by 逐层字段)]处理,然后进行直接调用. 详细说明 二.开窗函 ...
- 剑指Offer——斐波那契数列
题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.n<=39 分析: 递归解法肯定相当耗时. 因为当n=4时,程序是这样子递归运算的:Fibonacci( ...
- appium入门基础知识
1.概念区分: 1)IOS-UIAutomation:随着iOS4.0的发布,苹果公司同时发布了一个名为UIAutomation的测试框架,它可以用来在真实设备和iPhone模拟器上执行自动化测试 学 ...
- swift 值得学习的项目
http://www.php100.com/html/it/biancheng/2015/0112/8329.html