React Native数据存储

最近做RN开发中需要数据存储,查阅RN官方资料,发现推荐我们使用 AsyncStorage,对使用步骤做一下记录。

AsyncStorage是什么

  • 简单的,异步的,持久化的key-value存储系统
  • AsyncStorage在IOS下存储分为两种情况:
    • 存储内容较小,AsyncStorage会存储在序列化的字典中
    • 存储内容大,AsyncStorage会将数据单独存储在一个文件中
  • AsyncStorage在Android下,会将数据存储在SQLite或者RocksDB中,具体存储在哪里取决于设备支持哪一种方式

如何使用

安装
1.with npm:
shell 复制代码
npm install @react-native-async-storage/async-storage
2.如果是mac系统还需要pod-install一下:
shell 复制代码
npx pod-install
使用
js 复制代码
import AsyncStorage from '@react-native-async-storage/async-storage';

存储数据:

复制代码
static setItem(key: string, value: string, [callback]: ?(error: ?Error) => void)
js 复制代码
async saveData() {
    //用法一
    AsyncStorage.setItem(KEY, this.value, error => {
        error && console.log(error.toString());
    });

    //用法二
    AsyncStorage.setItem(KEY, this.value)
        .catch(error => {
            error && console.log(error.toString());
        });

    //用法三
    try {
        await  AsyncStorage.setItem(KEY, this.value);
    } catch (error) {
        error && console.log(error.toString());
    }
}

获取数据:

复制代码
static getItem(key: string, [callback]: ?(error: ?Error, result: ?string) => void)
js 复制代码
async getData() {
    //用法一
    AsyncStorage.getItem(KEY, (error, value) => {
        this.setState({
            showText: value
        });
        console.log(value);
        error && console.log(error.toString());
    });
    //用法二
    AsyncStorage.getItem(KEY)
        .then(value => {
            this.setState({
                showText: value
            });
            console.log(value);

        })
        .catch(error => {
            error && console.log(error.toString());
        });
    //用法三
    try {
        const value = await  AsyncStorage.getItem(KEY);
        this.setState({
            showText: value
        });
        console.log(value);
    } catch (error) {
        error && console.log(error.toString());
    }
}

删除数据:

复制代码
static removeItem(key: string, [callback]: ?(error: ?Error) => void)
js 复制代码
async removeData() {
    //用法一
    AsyncStorage.removeItem(KEY,error => {
        error && console.log(error.toString());
    });

    //用法二
    AsyncStorage.removeItem(KEY)
        .catch(error => {
            error && console.log(error.toString());
        });

    //用法三
    try {
        await  AsyncStorage.removeItem(KEY);
    } catch (error) {
        error && console.log(error.toString());
    }
}
相关推荐
英俊潇洒美少年42 分钟前
Vue 生产环境打包:SourceMap、压缩、混淆、加密全解 + 最佳实践
前端·javascript·vue.js
巴博尔1 小时前
UNIAPP中NVUE页面 动画
android·前端·javascript·ios·uni-app
猫头虎-前端技术2 小时前
JS 作用域与闭包:从变量提升到闭包陷阱的超详细解析
开发语言·javascript·云计算·bootstrap·ecmascript·openstack·perl
她说人狗殊途3 小时前
基于 vue-cli 创建
前端·javascript·vue.js
大家的林语冰4 小时前
Deno 2.8 正式发布,再次超越 Bun,史上最大的次版本升级诞生!
前端·javascript·node.js
影寂ldy5 小时前
C#数组的属性和方法(Clear / Copy / IndexOf )
开发语言·javascript·c#
Brave & Real5 小时前
小程序 const 在js中以及与同类的var和let之间的差异
javascript·微信小程序·小程序
米丘6 小时前
React 19.x 的 lazy 与 Suspense
前端·javascript·react.js
kyriewen6 小时前
手写虚拟DOM后,我反问面试官:key为什么不能用index?
前端·react.js·面试
ZC跨境爬虫6 小时前
跟着 MDN 学CSS day_21:(图像溢出控制与表单元素样式定制)
前端·javascript·css·ui·交互