javascript - 反应原生数据
问题描述
我是 React 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
}
}
推荐阅读
- mercurial - Does Mercurial push all local branches or only the working branch?
- oracle - 从 FieldA 取值,发送到 db 函数,返回值到 FieldB
- wordpress - How do I get the user_id after login wordpress/woocommerce
- c# - C#:动态链接库 (DLL) 初始化例程失败
- c++ - 使用 PCL 的 ROS 表面法线估计
- swift - 可以在不使用“静态”的情况下创建单例类吗?
- java - 如何组合来自同一个 char 数组的 2 个值?
- go - Bigtable 客户端初始化卡在 Kubernetes 容器中
- excel - Excel VBA:文件夹名称未知但扩展名已知的文件夹路径
- python - 重定向 django URL