Myk*_*kyi 3 database react-native redux
我想用 React Native 和 Redux 创建带有注释的待办事项列表。我有复杂的逻辑,将笔记和待办事项存储在不同的地方,具有不同的状态。我应该如何在 Android/IOS 设备的本地存储中存储所有关系和所有数据?
异步存储只能存储字符串数据,因此为了存储对象数据,您需要先将其序列化。对于可以序列化为 JSON 的数据,您可以在保存数据时使用 JSON.stringify() ,在加载数据时使用 JSON.parse() 。
import AsyncStorage from '@react-native-community/async-storage';
Run Code Online (Sandbox Code Playgroud)
存储字符串值
const storeData = async (value) => {
try {
await AsyncStorage.setItem('@storage_Key', value)
} catch (e) {
// saving error
}
}
Run Code Online (Sandbox Code Playgroud)
对对象值进行排序
const storeData = async (value) => {
try {
const jsonValue = JSON.stringify(value)
await AsyncStorage.setItem('@storage_Key', jsonValue)
} catch (e) {
// saving error
}
}
Run Code Online (Sandbox Code Playgroud)
读取字符串值
const getData = async () => {
try {
const value = await AsyncStorage.getItem('@storage_Key')
if(value !== null) {
// value previously stored
}
} catch(e) {
// error reading value
}
}
Run Code Online (Sandbox Code Playgroud)
读取对象值
const getData = async () => {
try {
const jsonValue = await AsyncStorage.getItem('@storage_Key')
return jsonValue != null ? JSON.parse(jsonValue) : null;
} catch(e) {
// error reading value
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10388 次 |
| 最近记录: |