【问题标题】:Which storage option is best for my react native application?哪种存储选项最适合我的 React Native 应用程序?
【发布时间】:2021-07-31 15:22:56
【问题描述】:

我正在构建一个需要保留一些值的 React Native 应用程序。但我不想为简单和小数据设置后端和云数据库而烦恼。我要保留的数据集就像 81 个数组项,每个数组项都有布尔值。 [true, true, false .... ] 要么 [{id: int, isCompleted: bool}, {id: int, isCompleted: bool},...]

由于我是 React Web 开发人员,如果它是 Web 应用程序,我会使用本地存储。但是在 React Native 中,我可以使用哪种类型的存储仅在设备本地?

【问题讨论】:

标签: react-native local-storage storage


【解决方案1】:

在您的情况下,最好的解决方案可能是使用 AsyncStorage 库。使用简单,可以直接存储在设备上。

import { AsyncStorage } from "react-native";

_store = async () => {
        try {
            await AsyncStorage.setItem('array', yourArray);
        } catch (error) {
            // Error
        }
    }

_rertrive = async () => {
        try {
            const value = await AsyncStorage.getItem('array');
            if (value !== null) {
                console.log(value);
            }
        } catch (error) {
            // Error
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-16
    • 2011-10-15
    • 1970-01-01
    • 2020-03-15
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多