React-Native之AsyncStorage和react-native-easy-toast

素颜马尾好姑娘i 2022-05-22 06:37 211阅读 0赞

1:常用方法

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对象。

2:带有的方法

  • static getItem(key:string , callback:(error,result)): 根据键来获取值,获取的结果会在回调函数中。
  • static setItem(key:string , value:string , callback:(error)): 设置键值对。
  • static removeItem(key:string , callback:(error)): 将根据键移出一项
  • static mergeItem:(key:string , value:string , callback:(error)): 合并现有的值和输入值。
  • static clear(callback:(error)): 清除所有的项目。
  • static getAllKeys(callback:(error)): 获取所有的键。
  • static multiGet(keys,callback:(errors,result)):获取多项,其中keys是字符串数组。
  • static multiSet(keyValuePairs,callback:(errors)):设置多项,其中keyValuePairs是字符串的二维数组。
  • static multiRemove(keys,callback(errors)):删除多项,其中keys是字符串数组。
  • static multiMerge(keyValuePairs,callback:(errors)):多个键值合并,其中keyValuePairs是字符串中的二维数组。

3:安装react-native-easy-toast(简单提示框)

  1. 地址:[https://github.com/crazycodeboy/react-native-easy-toast/blob/master/README.zh.md][https_github.com_crazycodeboy_react-native-easy-toast_blob_master_README.zh.md]

4:代码

  1. import React, { Component } from 'react';
  2. import {
  3. StyleSheet,
  4. Text,
  5. View,
  6. TextInput,
  7. AsyncStorage,
  8. } from 'react-native';
  9. import Toast,{DURATION} from 'react-native-easy-toast';//导入弹出框组件
  10. const KEY='text';//指定key
  11. type Props = {};
  12. export default class AsyncStorageTest extends Component {
  13. constructor(props) {
  14. super(props);
  15. }
  16. //保存
  17. save() {
  18. AsyncStorage.setItem(KEY, this.text, (error)=> { //设置键值对 key:value
  19. if (!error) {
  20. this.toast.show('保存成功', DURATION.LENGTH_LONG);//设置提示框,并设置持续时间
  21. } else {
  22. this.toast.show('保存失败', DURATION.LENGTH_LONG);
  23. }
  24. });
  25. }
  26. //取出
  27. get() {
  28. AsyncStorage.getItem(KEY, (error, result)=> {
  29. if (error) {
  30. this.toast.show('取出失败', DURATION.LENGTH_LONG);
  31. } else {
  32. if (result) {
  33. this.toast.show('取出的结果为:' + result, DURATION.LENGTH_LONG);
  34. } else {
  35. this.toast.show('没有找到对应的内容', DURATION.LENGTH_LONG);
  36. }
  37. }
  38. });
  39. }
  40. //移除
  41. remove(){
  42. AsyncStorage.removeItem(KEY,(error)=>{
  43. if (!error) {
  44. this.toast.show('移除成功', DURATION.LENGTH_LONG);
  45. } else {
  46. this.toast.show('移除失败', DURATION.LENGTH_LONG);
  47. }
  48. });
  49. }
  50. render() {
  51. return (
  52. <View>
  53. <TextInput style={
  54. {height:80}}
  55. onChangeText={(text)=> {
  56. this.text = text;
  57. }}
  58. />
  59. <View style={
  60. {flexDirection: 'row'}}>
  61. <Text style={styles.text} onPress={()=>this.save()}>
  62. 保存
  63. </Text>
  64. <Text style={styles.text} onPress={()=>this.remove()}>
  65. 移除
  66. </Text>
  67. <Text style={styles.text} onPress={()=>this.get()}>
  68. 取出
  69. </Text>
  70. </View>
  71. <Toast ref={toast=> {//提示框展示位置
  72. this.toast = toast
  73. }}/>
  74. </View>
  75. )
  76. }
  77. }
  78. const styles = StyleSheet.create({
  79. text: {
  80. fontSize: 20,
  81. margin:10
  82. }
  83. });

5:效果图

7070 1

发表评论

表情:
评论列表 (有 0 条评论,211人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ReactNative搭建环境

    天就帮助大家想做RN开发的朋友们整理一下搭建环境的步骤, 本文是根据官方文档整理的,只是本文会讲的更俗一点。 官方文档链接: * 中文:[https://r...