javascript - AsyncStorage 不会在组件加载时增加值
问题描述
我正在使用 AsyncStorage 和 React Native。我有一个在 useEffect 中调用的函数并且它可以工作,但是我的 AsyncStorage 值没有得到更新。它说“1”。我将 AsyncStorage 作为数组,我也尝试过这种传统方式,并且值不会在负载时增加。我已经在 IOS 模拟器和 IOS 设备上进行了测试,但没有运气。下面是在 useEffect((),[]) 中运行的函数
const addFunction = async () => {
var storage_array = await AsyncStorage.getItem(ASYNC_STORAGE_KEY);
try {
if(storage_array) {
storage_array = JSON.parse(storage_array);
let flow_complete = 0;
flow_complete++;
storage_array.push(flow_complete);
//storage_array.push(flow_test);
console.log('THIS IS flow_complete', flow_complete);
console.log('THIS IS THE ASYNCSTORAGE', storage_array);
} else {
flow_complete = 0;
console.log('Storage array is empty')
}
} catch (error) {
console.log(error);
}
}
解决方案
您需要设置项目以更新存储。
const addFunction = async () => {
var storage_array = await AsyncStorage.getItem(ASYNC_STORAGE_KEY);
try {
if(storage_array) {
storage_array = JSON.parse(storage_array);
let flow_complete = 0;
flow_complete++;
storage_array.push(flow_complete);
//storage_array.push(flow_test);
console.log('THIS IS flow_complete', flow_complete);
console.log('THIS IS THE ASYNCSTORAGE', storage_array);
AsyncStorage.setItem(ASYNC_STORAGE_KEY, JSON.stringify(storage_array));
} else {
flow_complete = 0;
console.log('Storage array is empty')
}
} catch (error) {
console.log(error);
}
}
推荐阅读
- android - 使用 android 获取我附近的地理参考点
- unix - 在 UNIX 中的多个位置复制相同的文件
- java - 将项目编译到 Jar 的时间分配给字符串/双精度值?
- jenkins - 来自 jenkins 的 sbt-release 没有用户输入
- javascript - TouchableHighlight underlayColor 不起作用。颜色只是没有改变
- sql-server - 发送 SQL Server 命令以从 Access 移动数据(在服务器上)
- arrays - 在 BigQuery 中加入与选择数组
- rest - 调用休息服务时与 SSL 证书相关的问题
- git - 托管在 Bitbucket 上的 Git 存储库.. 计算我推送提交的每一天
- python-3.x - 覆盖类方法。打印输出,包括 NONE