首页 > 解决方案 > 反应原生数据

问题描述

我是 React Native 的新手,我不知道如何存储数据。例如,我想制作一个应用程序,用户需要添加他的学校成绩并且保持注册以便将来查看它们。我该如何存储这个?(在数据库或设备中)我确实意识到这是一个菜鸟问题,但我搜索了很多,但找不到答案。

标签: javascriptandroidiosreact-native

解决方案


您可以使用@react-native-community/async-storage将数据存储在用户设备中以进行授权或其他用途。

存储数据

import { AsyncStorage } from 'react-native';
    
const storeData = async () => {
      try {
        await AsyncStorage.setItem('your_key', 'stored value')
      } catch (e) {
        // saving error
      }
    }

读取数据

import { AsyncStorage } from 'react-native';

const getData = async () => {
      try {
        const value = await AsyncStorage.getItem('your_key')
        if(value !== null) {
          // value previously stored 
        }
      } catch(e) {
        // error reading value
      }
    }

推荐阅读